uva12103 - Leonardo's Notebook 置换A^2=B

I just bought Leonardo's secret notebook! Rare object

collector Stan Ucker was really agitated but his friend,
special investigator Sarah Keptic was 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 nd 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 with W, 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 ercely 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
'.
SampleInput
2
QWERTYUIOPASDFGHJKLZXCVBNM
ABCDEFGHIJKLMNOPQRSTUVWXYZ
SampleOutput
No
Yes

  长度为n的循环节,如果n是奇数,那么一定能找到一个长度为n的A,使A^2=B。对于任意两个长度为n的的不相交循环B,C,都能找到一个长度为2n的循环A,A^2=BC。

  比如(a1a2a3)(a1a2a3)=(a1a3a2),(b1b2b3b4)(b1b2b3b4)=(b1b3)(b4)。

  所以这里可以找出所有循环节的长度,只要长度为奇数的不是奇数个都可以。

#include<iostream>
#include<algorithm>
#include<queue>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<set>
#include<map>
#define INF 0x3f3f3f3f
#define MAXN 2000010
#define MAXM 1500
#define MOD 1000000007
#define MAXNODE 8*MAXN
#define eps 1e-9
using namespace std;
typedef long long LL;
int T,vis[MAXN],cnt[MAXN];
char B[MAXN];
int main(){
    freopen("in.txt","r",stdin);
    scanf("%d",&T);
    while(T--){
        scanf("%s",B);
        memset(vis,0,sizeof(vis));
        memset(cnt,0,sizeof(cnt));
        for(int i=0;i<26;i++) if(!vis[i]){
            int j=i,n=0;
            do{
                vis[j]=1;
                j=B[j]-'A';
                n++;
            }while(j!=i);
            cnt[n]++;
        }
        int flag=1;
        for(int i=2;i<=26;i+=2) if(cnt[i]%2){
            flag=0;
            break;
        }
        if(flag) printf("Yes\n");
        else printf("No\n");
    }
    return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值