poj 3128 Leonardo's Notebook

来源:互联网 发布:期货农产品数据库建立 编辑:程序博客网 时间:2024/06/17 16:30

Description

— I just boughtLeonardo's secret notebook! Rare object collector Stan Ucker wasreally agitated but his friend, special investigator SarahKepticwas unimpressed.
— How do you know it is genuine?
— Oh, it must be, at that price. And it is written in the da Vincicode. Sarah browsed a few of the pages. It was obvious to her thatthe code was a substitution cipher, where each letter of thealphabet had been substituted by another letter.
— Leonardo would have written the plain-text and left it to hisassistant to encrypt, she said. And he must have supplied thesubstitution alphabet to be used. If we are lucky, we can find iton 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:
QWERTYUIOPASDFGHJKLZXCVBNM
— This may be Leonardo's instructions meaning that each A in theplain-text was to be replaced by Q, each B withW, etcetera. Let ussee... To their disappointment, they soon saw that this could notbe the substitution that was used in the book. Suddenly, Stanbrightened.
— Maybe Leonardo really wrote the substitution alphabet on the lastpage, and by mistake his assistant coded that line as he had codedthe rest of the book. So the line we have here is the result ofapplying some permutation TWICE to the ordinary alphabet! Sarahtook out her laptop computer and coded fiercely 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 dupedagain, my friend. In all probability, the book is a fake.

Write a program that takes a permutation of the English alphabet asinput and decides if it may be the result of performing somepermutation twice.

Input

The input beginswith a positive number on a line of its own telling the number oftest cases (at most 500). Then for each test case there is one linecontaining a permutation of the 26 capital letters of the Englishalphabet.

Output

For each test case,output one line containing Yes if the given permutation can resultfrom applying some permutation twice on the original alphabetstring ABC...XYZ, otherwise output No.

Sample Input

2QWERTYUIOPASDFGHJKLZXCVBNMABCDEFGHIJKLMNOPQRSTUVWXYZ

Sample Output

NoYes

Source

Northwestern Europe 2006
 
题目大意:给出一个A~Z的置换,问是否可以被表示为一个置换的平方(即G=G'*G')。
 
通过观察可以发现,一个置换乘上它本身,其中长度为偶数的循环节必然会分裂为两个长度相等的循环节,长度为奇数的循环节还是一个循环节,长度不变。如:
2341*2341=3412 (2341是一个循环节,平方后分裂为两个长度为2的循环节[13][24])
23451*23451=34512   (23451是一个循环节,平方后还是长度为5的循环节)
所以,给出的置换中长度为偶数的循环节必然是原置换中的循环节分裂出来的,而长度为奇数的循环节有可能是原置换中的循环节分裂出来的。因为只要判断能否被表示,所以可以忽略长度为奇数的循环节,只对长度为偶数的循环节进行考虑即可。
因为一个长度为偶数的循环节分裂出来的两个循环节的长度相等且同为原循环节长度的一半。所以,只要给出置换中所包含的长度为偶数的循环节能一一配对,那么就必然可以被表示为另一个置换的平方。
 
ACCODE

 

program pku_3128;

vard:array[1..26] of longint;

   p:array[1..26] of boolean;

   tot,i,size,now:longint;

   flag:boolean;

   s:string;

begin

 readln(tot);

 while tot>0 do

 begin

   readln(s); dec(tot);

   for i:=1 to 26 do p[i]:=false;

   for i:=1 to 26 do d[i]:=0;

   for i:=1 to 26 do

     if not(p[i]) then

     begin

       size:=1; p[i]:=true; now:=i;

       while not(p[ord(s[now])-ord('A')+1]) do

       begin

         now:=ord(s[now])-ord('A')+1;

         p[now]:=true; inc(size);

       end;

       inc(d[size]);

     end;

   flag:=true;

   for i:=2 to 26 do

     if i mod 2=0 then

       if d[i] mod 2=1 then flag:=false;

   if flag then writeln('Yes') else writeln('No');

 end;

end.

0 0