Physical Address

304 North Cardinal St.
Dorchester Center, MA 02124

Rotate the String CodeChef Solution

Problem -Rotate the String CodeChef Solution

This website is dedicated for CodeChef solution where we will publish right solution of all your favourite CodeChef problems along with detailed explanatory of different competitive programming concepts and languages.

Rotate the String CodeChef Solution in C++14

#include "bits/stdc++.h"
using namespace std;
typedef long long int ll;
int main() {
	ll t,m,n,p,j,i,ans;
	string s;
	cin>>t;
	while(t-->0){
	    cin>>s;
	    cin>>m>>p;
	    n=s.size();
	    vector<int> v(n);
	    v[0]=0;
	    for(i=1;i<n;i++){
	        j=v[i-1];
	        while(j>0 && s[i]!=s[j])
	        j=v[j-1];
	        if(s[i]==s[j]){
	            j++;
	        }
	        v[i]=j;
	    }
	    j=v[n-1];
	    int x=1;
	    if(j!=0 && n%(n-j)==0){
	        n=n-j;
	    }
	    ans=-1;
	    while(x<m+p){
	       if((x*n)%(m+p)==0){
	           ans=(x*n)/(m+p)*2;
	           break;
	       }
	       else if((x*n)%(m+p)==m){
	           ans=(x*n)/(m+p)*2+1;
	           break;
	       }
	       x++;
	    }
	    if(ans==-1){
	        ans=m+p;
	    }
	    cout<<ans<<endl;
	}
	return 0;
}

Rotate the String CodeChef Solution in C

#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stdlib.h>
#include<stdbool.h>
#define mod 1000000007
typedef long long int ll;
inline ll getn(){
	ll n=0, c=getchar();
	while(c < '0' || c > '9')
		c = getchar();
	while(c >= '0' && c <= '9')
		n = (n<<3) + (n<<1) + c - '0', c = getchar();
	return n;
}
ll gcd(ll a,ll b)
{ll t;
while(b)
{t=b;
b=a%b;
a=t;
}
return a;
}
ll power(ll a, ll b) 
{
	ll x = 1, y = a;
    while(b > 0) 
	{
        if(b%2 == 1) 
		{
            x=(x*y);
            if(x>mod) x%=mod;
        }
        y = (y*y);
        if(y>mod) y%=mod;
        b /= 2;
    }
    return x;
}
ll getptr(ll ptr ,ll m,ll n)
{
	if(!ptr)
	return n-m;
	
	else if(ptr-m<0)
	return (n+ptr-m);
	
	else
	return (ptr-m);
	
}
bool getvalue(char a[],ll ptr,ll n)
{
	ll i,end;
	if(!ptr)
	end=n-1;
	else
	end=ptr-1;
	
	for(i=0;i<=n/2&&a[i]==a[ptr++]&&a[n-1-i]==a[end--];i++)
	{
		if(ptr>=n)
		ptr=0;
		if(end<0)
		end=n-1;
	}
	if(i>n/2)
	return true ;
	else
	return false;
	
}
int main()
{int t;
t=getn();
	ll n,m,p,d,i,ca,n1,count,ptr;
	char a[500005];
while(t--)
{
	ptr=0;
	scanf("%s",a);
	n=strlen(a);
	m=getn();
	p=getn();
	count=1;
	ca=1;
	n1=m;
	ptr=getptr(ptr,n1,n);
	
	//printf("%lld \n",ptr);
	while(!getvalue(a,ptr,n))
	{
		
		count++;
		if(ca)
		{
			
			ca=0;
			n1=p;
			
		}
		else
		{
			ca=1;
			n1=m;
		}
		ptr=getptr(ptr,n1,n);
		//printf("%lld \n",ptr);
	}
	printf("%lld\n",count);
}
return 0;
}

Rotate the String CodeChef Solution in JAVA

/* package codechef; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
    public static int KMP(char[] txt, char[] pattern)
    {
        
        int[] lcp = lcp(pattern);
       // System.out.println(Arrays.toString(lcp));
        int i=1,j=0;
        while( i < txt.length && j<pattern.length)
        {
        if(pattern[j]==txt[i])
        {
            i++;
            j++;
        }
        else
        {
            if(j==0)
            i++;
            else
            j = lcp[j-1];
        }
        }
       // System.out.println(j);
        if( j==pattern.length)
         return i-j;
         else 
         return -1;
    
        
        }
    
    public static int[] lcp(char[] pattern)
    {
        int j=0;
        int[] lcp = new int[pattern.length];
        lcp[0]=0;
        for(int i=1;i<pattern.length;)
        {
            
            if( pattern[i]==pattern[j] )
            {
                lcp[i] =j+1;
                i++;
                j++;
            }
            else
            {
                if(j==0)
               { lcp[i]=0;
                i++;
                }else
                j = lcp[j-1];
            }
        }
        return lcp;
    }
	public static void main (String[] args) throws java.lang.Exception
	{
		// your code goes here
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int T = Integer.parseInt(br.readLine());
		for(int p=0;p<T;p++)
		{
		    
		    String line = br.readLine();
		    String[] h = br.readLine().split(" ");
		    int s1 = Integer.parseInt(h[0]);
		    int s2 = Integer.parseInt(h[1]);
		    String a = line + line;
		   // System.out.println(a);
		   int v  = KMP(a.toCharArray(),line.toCharArray());
		   //System.out.println(v);
	//	  int c =   2*v/(s1+s2);
		  /*
		  int sum = s1; 
		  int count =1;
		  count++;
		  sum += s1;
		  if(sum%v==0)
		  {
		      System.out.println(count);
		      break;
		  }
		      sum +=s2;
		    count++;
		   if(sum%v==0)
		   {
		         System.out.println(count);
		      break;
		       
		   }
		 */
		 int result=0;
		 int cycle=v;
		 int current=0;
		 int monkeyMove = s1;
		 int poMove =s2;
		 monkeyMove         %=       cycle;
        poMove             %=       cycle;
 
        BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));
		  while(true) {
 
            result++;
            current     =   current + monkeyMove;
            if(current >= cycle) {
                current -= cycle;
            }
            if(current  ==  0) {
                out.write(result+ " ");
                out.newLine();
                break;
            }
            result++;
            current     =   current + poMove;
            if(current >= cycle) {
                current -=  cycle;
            } if(current    ==  0) {
                out.write(result + " ");
                out.newLine();
                break;
            }
        }
		   
		    out.flush();
		}
	}
}

Rotate the String CodeChef Solution in C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ProgrammingContest.CodeChef.Contest.August_Cokk_off_2011 {
    class Rotate_the_String {
        static int gcd(int n, int r) { return r == 0 ? n : gcd(r, n % r); }

        const int MAX = 500000;
        static void Main() {
            int T = int.Parse(Console.ReadLine());

            uint[] table = new uint[MAX];
            uint[] pow = new uint[MAX];
            bool[,] chked = new bool[2, 500000];
            uint bs = 1000000009;

            while (T-- > 0) {
                string s = Console.ReadLine();
                //string s = "ab".PadLeft(500000, 'c');
                var xs = Array.ConvertAll<string, int>(Console.ReadLine().Split(' '), int.Parse);
                int m = xs[0],
                    p = xs[1];

                int n = s.Length;

                pow[0] = 1;
                table[0] = s[0];
                for (int i = 1; i < n; i++) {
                    pow[i] = pow[i - 1] * bs;
                    table[i] = table[i - 1] + s[i] * pow[i];
                }

                for (int i = 0; i < n; i++)
                    for (int j = 0; j < 2; j++)
                        chked[j, i] = false;

                for (int i = n, times = 1; ; ) {
                    for (int j = 0; j < 2; j++, times++) {
                        i -= xs[j];
                        if (i < 0) i += n;
                        if (i == 0 || chked[j, i]) {
                            Console.WriteLine(i == 0 ? times : -1);
                            goto NEXT;
                        }

                        chked[j, i] = true;
                        uint x = table[n - 1] * pow[i],
                             y = table[n - 1] - table[i - 1] + table[i - 1] * pow[n - 1] * bs;
                        //Console.WriteLine("{0}: {1} {2}", i, x, y);
                        if (x == y) {
                            Console.WriteLine(times);
                            goto NEXT;
                        }
                    }
                }

            NEXT: ;
            }
        }
    }
}
Rotate the String CodeChef Solution Review:

In our experience, we suggest you solve this Rotate the String CodeChef Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the Rotate the String CodeChef Solution.

Find on CodeChef

Conclusion:

I hope this Rotate the String CodeChef Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Programming Language in a business context; there are no prerequisites.

Keep Learning!

More Coding Solutions >>

Cognitive Class Answer

CodeChef Solution

Microsoft Learn

Leave a Reply

Your email address will not be published. Required fields are marked *