CCC '20 J4 - Cyclic Shifts

View as PDF

Submit solution

Points: 5 (partial)
Time limit: 1.0s
Memory limit: 512M

Problem type
Canadian Computing Competition: 2020 Stage 1, Junior #4

Thuc likes finding cyclic shifts of strings. A cyclic shift of a string is obtained by moving characters from the beginning of the string to the end of the string. We also consider a string to be a cyclic shift of itself. For example, the cyclic shifts of ABCDE are:


Given some text, T, and a string, S, determine if T contains a cyclic shift of S.

Input Specification

The input will consist of exactly two lines containing only uppercase letters. The first line will be the text T, and the second line will be the string S. Each line will contain at most 1000 characters.

For 6 of the 15 available marks, S will be exactly 3 characters in length.

Output Specification

Output yes if the text, T, contains a cyclic shift of the string, S. Otherwise, output no.

Sample Input 1


Output for Sample Input 1


Explanation of Output for Sample Input 1

CDEAB is a cyclic shift of ABCDE and is contained in the text ABCCDEABAA.

Sample Input 2


Output for Sample Input 2


Explanation of Output for Sample Input 2

The cyclic shifts of ABA are ABA, BAA, and AAB. None of these shifts are contained in the text ABCDDEBCAB.


  • 0
    Invalid  commented on Dec. 30, 2022, 12:36 a.m.

    My program seems to work on every test case I give it, but IndexErrors whenever i actually submit it on the first case that's not a test case. Could someone explain what's wrong? Thanks

    • 0
      fireheartjerry  commented on Jan. 1, 2023, 7:41 p.m. edited

      Your algorithm seems to be unnecessarily complicated for this problem. Try a solution that modifies the original string and checks operations on that.

  • 1
    960t  commented on Dec. 26, 2022, 8:05 p.m.

    when you TLE the very last test case :'(

  • -20
    TwoBigBlackHairyDonkeyBalls  commented on May 26, 2022, 7:41 p.m.

    This comment is hidden due to too much negative feedback. Show it anyway.

    • 9
      gavin_chen  commented on May 28, 2022, 8:47 p.m.

      says the one who failed 15 times after you solved it

  • 0
    darek  commented on Feb. 21, 2022, 4:20 a.m.

    When you see such an efficient and concise solution like the one by austin_1 (PY3). Facepalm...

  • 2
    gavin_chen  commented on Feb. 13, 2022, 2:18 p.m.

    is someone able to check my program, i do not understand why it is wrong Thanks!

    • 2
      Spitfire720  commented on Feb. 13, 2022, 2:44 p.m.

      You're failing sample test cases, please try those first before asking.

  • 5
    Badmode  commented on March 7, 2021, 11:44 p.m.

    What are the contraints for the length of S?

    • 5
      Nils_Emmenegger  commented on March 7, 2021, 11:58 p.m.

      Each line will contain at most 1000 characters.

      • 4
        Badmode  commented on March 8, 2021, 12:00 a.m.

        Thank you for clarifying.

  • 4
    devnarula  commented on March 25, 2020, 11:42 p.m.

    Can anyone explain what's wrong with my program?

    • 4
      ross_cleary  commented on March 26, 2020, 11:43 a.m.

      Hashing isn't required, and your algorithm checks if a permutation of S occurs in T, and not all permutations are cyclic shifts.