UVa12103
12103 Leonardo's Notebook
| I just bought Leonardo's secret notebook! Rare object col-
lector Stan Ucker was really agitated but his friend, special
investigator Sarah Keptic was unimpressed.
| How do you know it is genuine?
| Oh, it must be, at that price. And it is written in the da
Vinci code. Sarah browsed a few of the pages. It was obvious
to her that the code was a substitution cipher, where each
letter of the alphabet had been substituted by another letter.
|Leonardo would have written the plain-text and left it to
his assistant to encrypt, she said. And he must have supplied
the substitution alphabet to be used. If we are lucky, we can
nd it on the back cover!
She turned up the last page and, lo and behold, there was
a single line of all 26 letters of the alphabet:
QWERTY UIOPASDFGHJKLZXCV BNM
| This may be Leonardo's instructions meaning that each A in the plain-text was to be replaced
by Q, each B with W, etcetera. Let us see: : :
To their disappointment, they soon saw that this could not be the substitution that was used in the
book. Suddenly, Stan brightened.
| Maybe Leonardo really wrote the substitution alphabet on the last page, and by mistake his
assistant coded that line as he had coded the rest of the book. So the line we have here is the result of
applying some permutation TWICE to the ordinary alphabet!
Sarah took out her laptop computer and coded ercely for a few minutes. Then she turned to Stan
with a sympathetic expression.
| No, that couldn't be it. I am afraid that you have been duped again, my friend. In all probability,
the book is a fake.
Write a program that takes a permutation of the English alphabet as input and decides if it may
be the result of performing some permutation twice.
Input
The input begins with a positive number on a line of its own telling the number of test cases (at most
500). Then for each test case there is one line containing a permutation of the 26 capital letters of the
English alphabet.
Output
For each test case, output one line containing `Yes' if the given permutation can result from applying
some permutation twice on the original alphabet string ABC: : :XYZ, otherwise output `No'.
Universidad de Valladolid OJ: 12103 { Leonardo's Notebook 2/2
Sample Input
2
QWERTYUIOPASDFGHJKLZXCVBNM
ABCDEFGHIJKLMNOPQRSTUVWXYZ
Sample Output
No
Yes
题意:
给出26个大写字母的置换B,问是否存在置换A使得A^2=B。
分析:
两个长度为n的相同循环相乘,当n是奇数时结果也是一个长度为n的循环;当n是偶数时分裂为两个长度为n/2的循环。反过来说,任意一个长度为奇数n的循环B都可以找到一个长度为n的循环A使得A^2=B;任意两个长度为n的循环B、C都能找到一个长度为2n的循环A使得A^2=B*C。
我们将题目输入的B分解成不相交的循环的乘积。其中长度为偶数的循环一定要找到相同长度的另一循环配对才能满足题目的条件。

1 #include <cstdio> 2 #include <cstring> 3 int main(){ 4 char B[30]; 5 int vis[30],cnt[30],T; scanf("%d",&T); 6 while(T--){ 7 scanf("%s",&B); 8 memset(vis,0,sizeof vis),memset(cnt,0,sizeof cnt); 9 for(int i = 0 ; i < 26 ; i++)if(!vis[i]){ 10 int j = i,n = 0; 11 do vis[j] = 1,j = B[j] - 'A',n++; 12 while(j != i); 13 cnt[n]++; 14 } 15 int ok = 1; 16 for(int i = 2 ; i <= 26 ; i += 2)if(cnt[i] & 1) ok = 0; 17 if(ok) printf("Yes\n"); 18 else printf("No\n"); 19 } 20 return 0; 21 }