POJ 3128 Leonardo's Notebook(置换)

Leonardo’s Notebook

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 1514 Accepted: 645

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

Northwestern Europe 2006      

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次方。

 

也就是如果一个置换可以表示成另外一个置换的平方,那么这个置换循环长度为偶数的必须为偶数个。

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
using namespace std;
const int MAXN=30;
char str[MAXN];
int a[MAXN];
bool vis[MAXN];
int cnt[MAXN];
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%s",&str);
        for(int i=0;i<26;i++)
           a[i]=str[i]-'A';
        memset(vis,false,sizeof(vis));
        memset(cnt,0,sizeof(cnt));
        int num;
        for(int i=0;i<26;i++)
        {
            if(!vis[i])
            {
                vis[i]=true;
                num=1;
                int t=a[i];
                while(!vis[t])
                {
                    num++;
                    vis[t]=true;
                    t=a[t];
                }
                cnt[num]++;
            }
        }
        bool flag=true;
        for(int i=2;i<27;i+=2)//偶数长度的必须是偶数个
        {
            if(cnt[i]%2!=0){flag=false;break;}
        }
        if(flag)printf("Yes\n");
        else printf("No\n");
    }
    return 0;
}

 

    原文作者:kuangbin
    原文地址: https://www.cnblogs.com/kuangbin/archive/2012/09/04/2670578.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞