Note:
A dna sequence is ring, which means that the sequence with N bases has n Representation Methods (assuming there is no repetition ). The N sequences have a minimum representation, which means that the Lexicographic Order of the sequence is the smallest (the Lexicographic Order means the size of the dictionary, such as ABC <ACB, B <BCD, EF <G)
Method: the size of the generated sequence can be compared from any two locations in a sequence. Then use this comparison method to find the minimum value.
# Include <iostream> using namespace STD; # define Max 105int lessthan (char s [], int P, int q) // The size of a sequence starting from P and starting from q {long Len = strlen (s); For (INT I = 0; I <Len; I ++) {If (s [(p + I) % Len]! = S [(q + I) % Len]) return s [(p + I) % Len] <s [(q + I) % Len]; // compare size} return 0;} int main () {int t; CIN> T; while (t --) {char s [Max]; CIN> S; long n = strlen (s); int flag = 0; For (INT I = 0; I <n; I ++) // It is equivalent to finding the smallest element in the array {If (lessthan (S, I, flag) Flag = I ;}for (INT I = 0; I <n; I ++) cout <s [(flag + I) % N]; cout <Endl ;}}