[ACM] poj 3128 Leonardo's Notebook (置换群,循环节)

286 篇文章 140 订阅
41 篇文章 0 订阅

Leonardo's Notebook
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 1791 Accepted: 787

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;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值