【poj 3128】置換羣 循環節

Leonardo's Notebook
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 1858   Accepted: 817

Description

— I just bought Leonardo's secret notebook! Rare object collector Stan Ucker was really agitated but his friend, special investigator Sarah Kepticwas 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 find 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: 
QWERTYUIOPASDFGHJKLZXCVBNM 
— This may be Leonardo's instructions meaning that each A in the plain-text was to be replaced by Q, each B withW, 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 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 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.

Sample Input

2
QWERTYUIOPASDFGHJKLZXCVBNM
ABCDEFGHIJKLMNOPQRSTUVWXYZ

Sample Output

No
Yes

Source



解題思路(轉載):

1.任意一個長爲 L 的置換的k次冪,會把自己分裂成gcd(L,k) 分, 並且每一份的長度都爲 L / gcd(l,k)

2.假如 d = gcd(L,K),l = L / gcd(L,k),那麼我們只需要找到d個長爲l的循環,將他們交錯循環連接成一個長爲 d * l 的大循環, 這樣一個過程就相當於開k次方。

題目大意:給出一個A~Z的置換,問是否可以被表示爲一個置換的平方(即G=G'*G')。 
  
通過觀察可以發現,一個置換乘上它本身,其中長度爲偶數的循環節必然會分裂爲兩個長度相等的循環節,長度爲奇數的循環節還是一個循環節,長度不變。如: 
2341*2341=3412  (2341是一個循環節,平方後分裂爲兩個長度爲2的循環節[13][24]) 
23451*23451=34512    (23451是一個循環節,平方後還是長度爲5的循環節) 
所以,給出的置換中長度爲偶數的循環節必然是原置換中的循環節分裂出來的,而長度爲奇數的循環節有可能是原置換中的循環節分裂出來的。因爲只要判斷能否被表示,所以可以忽略長度爲奇數的循環節,只對長度爲偶數的循環節進行考慮即可。 
因爲一個長度爲偶數的循環節分裂出來的兩個循環節的長度相等且同爲原循環節長度的一半。所以,只要給出置換中所包含的長度爲偶數的循環節能一一配對,那麼就必然可以被表示爲另一個置換的平方。 

關鍵:循環節長度爲偶數的循環節個數必須爲偶數。

#include <iostream>
#include <string.h>
using namespace std;
const int maxn=28;
int num[maxn];
int vis[maxn];
int loop[maxn];

int main()
{
  int t;cin>>t;
  string s;
  while(t--)
  {
    memset(vis,0,sizeof(vis));
    memset(loop,0,sizeof(loop));
    cin>>s;
    for(int i=0;i<26;i++)
      num[i+1]=s[i]-'A'+1;
    for(int i=1;i<=26;i++)
    {
      int count=0,tag;
      tag=i;
      while(!vis[tag])
      {
        count++;//每個循環節的長度
        vis[tag]=1;
        tag=num[tag];
      }
      loop[count]++;//長度爲count的循環節的個數++
    }
    int flag=0;
    for(int i=2;i<=26;i+=2)
      if(loop[i]%2==1)//長度爲偶數的循環節有奇數個
    {
      flag=1;
      break;
    }
    if(flag)
      cout<<"No"<<endl;
    else
      cout<<"Yes"<<endl;
  }
  return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章