POJ 3128 Leonardo's Notebook(置换的幂与分裂)

传送门:http://poj.org/problem?id=3128

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

题目意思是:一个置换是否可以由另一个置换的平方得来的。一个置换的平方,原来偶数长的循环会被分裂成两段长度相等的循环,而奇数长的循环不会被分裂。题目只是问是否存在,所以只要看所给置换中偶数长的循环是否成对,否则就不能由一个置换的平方得来。

补充:
假如是一个长度为奇数的置换T,T2是不会发生分裂的,得到的还是一个长度为奇数的置换
假如是一个长度为偶数的置换T,T2是会分裂成两个长度相等的置换
得到两个长度为偶数的置换
得到两个长度为奇数的置换
仔细观察一下,在最后得到的所有置换中,偶数长度的置换一定是成对存在的
即如果当前是置换的大小为偶数,那么他一定是由某个置换分裂成的,一定是成对出现的

所以我们只需要判断所有大小为偶数的置换是否成对出现即可,成对出现输出Yes,反之输出No;

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<set>
#include<map>
#include<stack>
#include<queue>
#include<ctype.h>
#include<vector>
#include<algorithm>
#include<sstream>
#define PI acos(-1.0)
using namespace std;
typedef long long ll;
typedef unsigned long long llu;
const int inf = 0x3f3f3f3f;
const ll lnf = 0x3f3f3f3f3f3f3f3f;
const int maxn = 100;
char s[maxn];
int a[maxn],vis[maxn],c[maxn];
int solve()
{
    int res,cnt;
    for(int i=1;i<=26;i++){
        if(vis[i]){
            vis[i] = 0;
            cnt = 1;
            int j = a[i];
            while(j!=i){
                vis[j] = 0;
                j = a[j];
                cnt++;
            }
            c[cnt]++;
        }
    }
    for(int i=2;i<27;i+=2)
        if(c[i]%2)    return 0;
    return 1;
}
int main(void)
{
    int n;
    while(~scanf("%d",&n)){
        while(n--){
            scanf("%s",s+1);
            for(int i=1;i<=26;i++){
                a[i] = s[i] + 1 - 'A';
                vis[i] = 1,c[i] = 0;
            }
            if(solve()) printf("Yes\n");
            else printf("No\n");
        }
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

逃夭丶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值