NECKDEC - Necklace Decomposition
The set of cyclic rotations of a string are the strings obtained by embedding the string clockwise on a ring, with the first character following on the last, starting at any character position and moving clockwise on the ring until the character preceeding the starting character is reached. A string is a necklace if it is the lexicographically smallest among all its cyclic rotations. For instance, for the string 01011 the cyclic rotations are (10110,01101,11010,10101,01011), and furthermore 01011 is the smallest string and hence, a necklace.
Input
On the first line of the input is a single positive integer n, telling the number of test scenarios to follow. Each scenario consists of one line containing a non-empty string of zeros and ones of length at most 100.
Output
For each scenario, output one line containing the necklace decomposition of the string. The necklaces should be written as ’(’ necklace ’)’.
Example
Input: 5 0 0101 0001 0010 11101111011 Output: (0) (0101) (0001) (001)(0) (111)(01111)(011)
hide comments
Oleg:
2010-08-10 17:20:54
It will be more fun if text limit will be ~100000. Such problem has O(N) solution. |
|
David Gómez:
2010-08-09 04:10:26
Maybe you should try MINMOVE first |
Added by: | Mohammad Kotb |
Date: | 2010-08-08 |
Time limit: | 1s |
Source limit: | 50000B |
Memory limit: | 1536MB |
Cluster: | Cube (Intel G860) |
Languages: | All except: OBJC VB.NET |
Resource: | NCPC 2005 |