Ultraviolet A 12103-Leonardo's notebook
Question Link
Assume that a letter is given to replace B and check whether a causes a ^ 2 = B.
Idea: Any k power of replacement with a length of L splits itself into gcd (L, k) points, and each part has a length of L/gcd (L, k), so the square does not change the length of an odd number, and the even number is split into two cycles with the same length. Therefore, if the number of cycles with an even number in B is not an even number, it is inevitable that a does not exist.
Code:
# Include <stdio. h> # include <string. h> const int n = 30; int T, next [N], vis [N], num [N]; char STR [N]; bool judge () {for (INT I = 2; I <= 26; I + = 2) if (Num [I] % 2) return false; return true;} int main () {scanf ("% d", & T); While (t --) {scanf ("% s", STR); For (INT I = 0; I <26; I ++) next [I] = STR [I]-'A'; memset (VIS, 0, sizeof (VIS); memset (Num, 0, sizeof (Num); For (INT I = 0; I <26; I ++) {If (! Vis [I]) {vis [I] = 1; int T = next [I]; int CNT = 1; while (! Vis [T]) {vis [T] = 1; t = next [T]; CNT ++;} num [CNT] ++ ;}} printf ("% s \ n", Judge ()? "Yes": "no");} return 0 ;}