Leonardo's Notebook [NWERC 2006,LA 3641]
2016-06-08 13:26
246 查看
题目地址请点击——
— 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:
QWERTY UIOPASDFGHJKLZXCV BNM
— 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 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.
输入第一行为数据组数 T(T<=500)。
以下每行为一组数据,即一个长度为 26 的字符串,也就是大写字母的一个置换。
对于每组数据,如果存在 A,输出 Yes,否则输出 No。
QWERTYUIOPASDFGHJKLZXCVBNM
ABCDEFGHIJKLMNOPQRSTUVWXYZ
Yes
D=(a1 a2 a3 … an) 且 n 为奇数
∴D2=(a1 a3 a5 … an a2 a4 … an−1)
所以,若某个循环的元素个数为奇数,那么它一定可以表示成为一个与它元素个数相同的一个循环的平方。
我们设 E 为一个为一个大写字母的循环且其元素个数为偶数,即
E=(b1 b2 b3 … bn) 且 n 为偶数
∴E2=(b1 b3 b5 … bn−1)(b2 b4 b6 … bn)
所以,若两个循环的元素个数相同且均为偶数,那么它们的乘积一定可以表示成为一个元素个数为它们一半的一个循环的平方。
反之,若一个循环 F 的元素个数为奇数,那么一定存在一个与它元素个数相同的一个循环 G 使得 G2=F。
若一个循环的元素个数为偶数,则其无法表示成为一个循环的平方,除非有另一个与其不相交且元素个数与其相同的循环与其相乘,才能使得它们的乘积为一个循环的平方。
所以,我们可以把题干中的 B 给循环分解,看元素个数为偶数的循环是否可以一一配对(配对的双方必须元素个数相同),然后就可以判断这所有的循环的乘积 B 是否能表示成为 A2 了。
Leonardo’s Notebook
Description [English]
— 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 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:
QWERTY UIOPASDFGHJKLZXCV BNM
— 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 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.
Description [Chinese]
给出 26 个大写字母的置换 B,问是否存在一个置换 A,使得 A2=B。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.输入第一行为数据组数 T(T<=500)。
以下每行为一组数据,即一个长度为 26 的字符串,也就是大写字母的一个置换。
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’.对于每组数据,如果存在 A,输出 Yes,否则输出 No。
Sample Input
2QWERTYUIOPASDFGHJKLZXCVBNM
ABCDEFGHIJKLMNOPQRSTUVWXYZ
Sample Output
NoYes
Solution
我们设 D 为一个大写字母的循环且其元素个数为奇数,即D=(a1 a2 a3 … an) 且 n 为奇数
∴D2=(a1 a3 a5 … an a2 a4 … an−1)
所以,若某个循环的元素个数为奇数,那么它一定可以表示成为一个与它元素个数相同的一个循环的平方。
我们设 E 为一个为一个大写字母的循环且其元素个数为偶数,即
E=(b1 b2 b3 … bn) 且 n 为偶数
∴E2=(b1 b3 b5 … bn−1)(b2 b4 b6 … bn)
所以,若两个循环的元素个数相同且均为偶数,那么它们的乘积一定可以表示成为一个元素个数为它们一半的一个循环的平方。
反之,若一个循环 F 的元素个数为奇数,那么一定存在一个与它元素个数相同的一个循环 G 使得 G2=F。
若一个循环的元素个数为偶数,则其无法表示成为一个循环的平方,除非有另一个与其不相交且元素个数与其相同的循环与其相乘,才能使得它们的乘积为一个循环的平方。
所以,我们可以把题干中的 B 给循环分解,看元素个数为偶数的循环是否可以一一配对(配对的双方必须元素个数相同),然后就可以判断这所有的循环的乘积 B 是否能表示成为 A2 了。
Code
#include <iostream> #include <cstdio> #include <cstring> using namespace std; int T; char s[30]; int nxt[30],tot[30]; bool in_stack[30]; void dfs(int now,int cnt){ in_stack[now]=true; if(in_stack[nxt[now]]){ if(!(cnt&1))tot[cnt/2]++; } else dfs(nxt[now],cnt+1); } int main(){ scanf("%d",&T); for(int i=1;i<=T;i++){ memset(in_stack,0,sizeof in_stack); memset(tot,0,sizeof tot); scanf("%s",s); for(int j=0;j<26;j++)nxt[j+1]=s[j]-'A'+1; for(int j=1;j<=26;j++)if(!in_stack[j])dfs(j,1); for(int j=1;j<=13;j++)if(tot[j]&1){printf("No\n");goto nxt;} printf("Yes\n");nxt:; } return 0; }
相关文章推荐
- jboss配置https
- MySQL replace into DUPLICATE
- python基础随笔
- EventBus实现广播的接受
- iOS学习应用开发就业课:第7章_056:UIView动画基础
- Spring事务传播
- java多线程完美篇
- 典型用户
- uniq命令注意事项,检查重复行的时候,只会检查相邻的行。
- 安装tomcat过程中出现问题小结
- ViewPager 详解(一)---基本入门
- ubuntu在shell下用默认程序打开文件
- 理解闭包 js回收机制
- ActiveMQ实例2--Spring JMS发送消息
- python案例-用户登录
- JNI函数方法
- js公有、私有、静态属性和方法的区别
- Apache 网站301重定向
- C语言小游戏开发
- hjr-DSP:关键字与伪指令