CodeChef is a non-commercial competitive programming community
Login
Username (New User? Signup) Password (Forgot Password?)
Signup
Login or
Signup with
Connect
Note
  • Publicize your achievements on your Facebook Wall.
  • Challenge your friends or ask them for help.

Site Navigation

  • PRACTICE
    • Easy
    • Medium
    • Hard
    • Challenge
    • Peer
  • COMPETE
    • All Contests
    • June Long 2012
    • May Cook-Off
    • May Long 2012
  • DISCUSS
    • Forums
    • Blog
    • Wiki
    • Facebook
    • Twitter
  • COMMUNITY
    • CodeChef Meetups
    • Campus Chapters
    • Host your Contest
    • User Groups
    • CodeChef TechTalks
    • All Educational Initiatives
  • HELP
    • Frequently Asked Questions
    • FAQ for problem setters
    • Problem Setting
    • Tutorials
    • Long Contest Ranks
    • Short Contest Ranks
    • Event Calendar
  • ABOUT
    • About CodeChef
    • Team CodeChef
    • Press Room
    • CodeChef Financials
    • CodeChef Sponsorships
    • CEO's Corner
    • Contact Us
    • About Directi
Home » Practice(medium) » Rotate the String

Rotate the String

Problem code: ROTSTRNG

  • Submit
  • All Submissions

All submissions for this problem are available.

Did you know that a cluster of bananas can also be called a 'hand' of bananas ? No points for guessing what the individual bananas are called. ya.. 'fingers' :). The monkey from the valley of food is all set to fight for the title 'Dragon Chef' after having a hand of bananas for breakfast. As you know that the judge, master Oogway is always late for the show, the monkey and Po decided to play a game. They took a string of characters and started rotating it.

A k-rotation on a string takes the trailing k characters of the string and attaches it to the beginning of the string in the same order. For eg. 3-rotation on the string "noodles" results in the string "lesnood". On the given string S, the monkey performs a M-rotation and gives it to Po. Po performs a P-rotation on it and gives back to the monkey. The monkey again performs a M-rotation on it and so on... Though Shifu is getting angry watching the monkey play with Po, he is wondering after how many minimum (non-zero) number of rotations we can get back to the original string S. This can happen after a rotation performed by the monkey or Po. Please find it for him.

Input

First line contains T ( number of test cases, around 10 ). T cases follow. Each case consists of two lines. First line has the string S of length n ( 1 <= n <= 500,000 ), having ('a'-'z' , 'A'-'Z'). Second line contains two integers M P separated by a space ( 1 <= M, P <= n ). Comparisons are case sensitive i.e., 'a' is not equal to 'A'.

Output

For each test case, output the answer in a new line. If its impossible to get back to the original string by performing the rotations as mentioned above, print -1

Example

Input:
2
AbcDef
1 2
abcabc
1 1

Output:
4
3

Explanation Case 1 : Monkey : fAbcDe , Po : DefAbc, Monkey : cDefAb , Po : AbcDef Case 2 : Monkey : cabcab , Po : bcabca, Monkey : abcabc

Warning : Large input / output. You may have to use efficient input / output methods if you are using languages with heavy input / output overload. Eg: Prefer using scanf/printf to cin/cout for C/C++


Author: flying_ant
Date Added: 6-08-2011
Time Limit: 2 sec
Source Limit: 50000 Bytes
Languages: ADA, ASM, BASH, BF, C, C99 strict, CAML, CLOJ, CLPS, CPP 4.0.0-8, CPP 4.3.2, CS2, D, ERL, F#, FORT, GO, HASK, ICK, ICON, JAR, JAVA, JS, LISP clisp, LISP sbcl, LUA, NEM, NICE, PAS fpc, PAS gpc, PERL, PERL6, PHP, PIKE, PRLG, PYTH, PYTH 3.1.2, RUBY, SCALA, SCM guile, SCM qobi, ST, TCL, TEXT, WSPC


  • Submit

Comments

  • Login or Register to post a comment.

my code is working on my PC

jastiger @ 27 Aug 2011 11:56 AM
my code is working on my PC and also giving correct results .. which cases according to you, my code is not giving right results ??

can u please please check my

r1rajiv92 @ 12 Oct 2011 08:53 PM
can u please please check my code n tell me what wrong with it. in my pc i get the right answer but here its giving me wrong answer. #include #include int main() { int n,k,i,y,q,chk=0; scanf("%d",&n); char ch[500000]=" ",ch1[500000]=" ",ch2[500000]=" "; while(n--) { int m,p,count=0; scanf("%s",&ch); strcpy(ch1,ch); scanf("%d%d",&m,&p); k=1; q=m; while(k!=0) { y=0; for(i=strlen(ch1)-q;ch1[i]!='';i++) { ch2[y]=ch1[i]; y++; } ch1[strlen(ch1)-q]=''; ch2[y]=''; for (i=strlen(ch1)-1;i>=0;i--) ch1[i+y]=ch1[i]; ch1[strlen(ch1)+y]=''; for(i=0;i10000) { count=-1; break; } } printf("%d",count); } return 0; }

can you please see my

digs2digi @ 16 Oct 2011 10:44 PM
can you please see my solution,why am I getting the wrong answer..In my pc it is working fine

http://www.codechef.com/views

digs2digi @ 16 Oct 2011 10:49 PM
http://www.codechef.com/viewsolution/704768

in my submissions,why it is

digs2digi @ 16 Oct 2011 11:07 PM
in my submissions,why it is showing time=0.0 and still showing wrong answer?what does time=0 mean?Did my code even ran?

Hi.. Can some one suggest me

shwetanka_19 @ 6 Nov 2011 11:04 PM
Hi.. Can some one suggest me when could be the case of '-1' as the answer?

I can't think of a case where

mrh4102 @ 13 Nov 2011 08:06 AM
I can't think of a case where '-1' will be the output.

does time limit exceeded

sumit4iit @ 31 Dec 2011 04:54 PM
does time limit exceeded means that my ans was correct??

acdaa 1 2 Should give

ankitdbst @ 6 Jan 2012 03:03 PM
acdaa 1 2 Should give -1. @admin I have tested quite a few test cases. Can you tell me which test cases is my solution id 781831 is failing?

SUCCESSFUL SUBMISSIONS FOR THIS PROBLEM:

Programming Competition Fetching successful submissions
Directi Go for Gold

HELP

Program should read from standard input and write to standard output. After you submit a solution you can see your results by clicking on the [My Submissions] tab on the problem page. Below are the possible results:

 

  • Accepted Your program ran successfully and gave a correct answer. If there is a score for the problem, this will be displayed in parenthesis next to the checkmark.
  • Time Limit Exceeded Your program was compiled successfully, but it didn't stop before time limit. Try optimizing your approach.
  • Wrong Answer Your program compiled and ran succesfully but the output did not match the expected output.
  • Runtime Error Your code compiled and ran but encountered an error. The most common reasons are using too much memory or dividing by zero. For the specific error codes see the help section.
  • Compilation Error Your code was unable to compile. When you see this icon, click on it for more information.
  • If you are still having problems, see a sample solution here.

CodeChef is a global programming communityCodeChef hosts online programming competitions
CodeChef is a non-commercial competitive programming community
  • About CodeChef
  • About Directi
  • CEO's Corner
  • C-Programming
  • Programming Languages
  • Contact Us
© 2009 Directi Group. All Rights Reserved. CodeChef uses SPOJ © by Sphere Research Labs
In order to report copyright violations of any kind, send in an email to copyright@codechef.com
CodeChef a product of Directi
The time now is:
CodeChef - A Platform for Aspiring Programmers

CodeChef was created as a platform to help programmers make it big in the world of algorithms, computer programming and programming contests. At CodeChef we work hard to revive the geek in you by hosting a programming contest at the start of the month and another smaller programming challenge in the middle of the month. We also aim to have training sessions and discussions related to algorithms, binary search, technicalities like array size and the likes. Apart from providing a platform for programming competitions, CodeChef also has various algorithm tutorials and forum discussions to help those who are new to the world of computer programming.

Practice Section - A Place to hone your 'Computer Programming Skills'

Try your hand at one of our many practice problems and submit your solution in a language of your choice. Our programming contest judge accepts solutions in over 35+ programming languages. Preparing for coding contests were never this much fun! Receive points, and move up through the CodeChef ranks. Use our practice section to better prepare yourself for the multiple programming challenges that take place through-out the month on CodeChef.

Compete - Monthly Programming Contests and Cook-offs

Here is where you can show off your computer programming skills. Take part in our 10 day long monthly coding contest and the shorter format Cook-off coding contest. Put yourself up for recognition and win great prizes. Our programming contests have prizes worth up to Rs.20,000 and $700lots more CodeChef goodies up for grabs.

Discuss

Are you new to computer programming? Do you need help with algorithms? Then be a part of CodeChef's Forums and interact with all our programmers - they love helping out other programmers and sharing their ideas. Have discussions around binary search, array size, branch-and-bound, Dijkstra's algorithm, Encryption algorithm and more by visiting the CodeChef Forums and Wiki section.

CodeChef Community

As part of our Educational initiative, we give institutes the opportunity to associate with CodeChef in the form of Campus Chapters. Hosting online programming competitions is not the only feature on CodeChef. You can also host a coding contest for your institute on CodeChef, organize an algorithm event and be a guest author on our blog.

Go For Gold

The Go for Gold Initiative was launched about a year after CodeChef was incepted, to help prepare Indian students for the ACM ICPC World Finals competition. In the run up to the ACM ICPC competition, the Go for Gold initiative uses CodeChef as a platform to train students for the ACM ICPC competition via multiple warm up contests. As an added incentive the Go for Gold initiative is also offering over Rs.8 lacs to the Indian team that beats the 29th position at the ACM ICPC world finals. Find out more about the Go for Gold and the ACM ICPC competition here.

Domain Name Registration, Web hosting, and Website Design provided by BigRock.com