Dog Girls

View as PDF

Submit solution

Points: 20 (partial)
Time limit: 1.4s
Memory limit: 256M

Author:
Problem types
Allowed languages
Ada, Assembly, Awk, Brain****, C, C#, C++, COBOL, CommonLisp, D, Dart, F#, Forth, Fortran, Go, Groovy, Haskell, Intercal, Java, JS, Kotlin, Lisp, Lua, Nim, ObjC, OCaml, Octave, Pascal, Perl, PHP, Pike, Prolog, Python, Racket, Ruby, Rust, Scala, Scheme, Sed, Swift, TCL, Text, Turing, VB, Zig

This morning, you woke up and realized that some dogs have turned into girls! In light of this exciting event, you immediately put on your wizard robes and walk outside, as everybody knows that dog-girls are magic users. Sure enough, one such sorceress is casting a spell on the street, and struck by awe, you gaze at her intently (so you can see what spell she's casting, of course).

For a dog-girl to use cast a spell, she must speak a magic word (which uniquely corresponds to that spell). A word S is magic if and only if |S| \ge 2 and you can rotate it left by at least one but less than |S| positions such that the rotated word forms the original word. More formally, a word S is magic if and only if there exists an index i (0 < i < |S|) such that S_{(i+j) \bmod |S|} = S_j (0 \le j < |S|) (the first letter of S is at index 0).

You hear the sorceress dog-girl mumble a long sequence of lowercase English letters, and you wonder what spell(s) she cast just now. Because you could not discern where the words began and ended when she spoke, you will have to settle for finding out how many distinct spells she could have cast.

Input Specification

The first and only line will have S, the sequence of letters the dog-girl mumbled (1 \le |S| \le 5000).

At least 10% of the test cases will have 1 \le |S| \le 100.

Another 15% of the test cases will have 1 \le |S| \le 400.

Another 25% of the test cases will have 1 \le |S| \le 1000.

Another 20% of the test cases will have 1 \le |S| \le 3000.

Output Specification

The first and only line of output should contain the number of distinct substrings of S that are magic words.

Sample Input 1

abracadabracad

Sample Output 1

1

Explanation for Sample Input 1

The whole string is a magic word: abracadabracad. Additionally, no proper substring of abracadabracad is a magic word.

Sample Input 2

abbccbba

Sample Output 2

2

Explanation for Sample Input 2

bb and cc are magic words.


Comments


  • 3
    pblpbl  commented on March 28, 2020, 1:36 p.m. edited

    can this be done in n^2 time and n^2 space? edit: nvm I did it


  • 15
    echofox  commented on April 23, 2018, 9:19 a.m.

    protip: don't open this question in public


    • 6
      pblpbl  commented on March 26, 2020, 4:57 p.m.

      i needed to read this before scrolling past it


  • -24
    kobortor  commented on Feb. 22, 2016, 6:43 p.m.

    This comment is hidden due to too much negative feedback. Click here to view it.


    • 5
      d  commented on Feb. 27, 2016, 7:39 p.m.

      Just use the prime number 2^{53}+5.