博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ 3128 Leonardo's Notebook (置换)
阅读量:6242 次
发布时间:2019-06-22

本文共 3952 字,大约阅读时间需要 13 分钟。

Leonardo's Notebook
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 2324   Accepted: 988

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

2QWERTYUIOPASDFGHJKLZXCVBNMABCDEFGHIJKLMNOPQRSTUVWXYZ

Sample Output

NoYes

Source

 

题意:

  给你一串大写的英文字母, 问你能不能通过2次置换ABCD···XYZ使得变成你要的字母串。

题解:

  如果存在一个置换 (a1, a2, a3)。那么 (a1, a2, a3)(a1, a2, a3) = (a1, a3, a2)。

  如果存在一个置换(b1, b2, b3, b4)。那么 (b1, b2, b3, b4)(b1, b2, b3, b4) = (b1, b3)(b2, b4)

  证明:

(a1, a2, a3)表示的是 a1 -> a2 , a2 -> a3 , a3 -> a1。

(需要学习置换的乘法)

当任意两个长度为n(奇数)的置换,都可以找到一个置换A , 满足A^2 = B。

 

当任意两个不相交的长度为n(奇数偶数都可以)循环置换 B, C ,都能找到一个长度为2n的循环置换A, 满足 A^2 = B C。

 

所以只要长度为偶数的置换有偶数个就可以输出Yes。

 

1 #include 
2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8 #include
9 #include
10 #include
11 using namespace std;12 typedef long long LL;13 #define ms(a, b) memset(a, b, sizeof(a))14 #define pb push_back15 #define mp make_pair16 const int INF = 0x7fffffff;17 const int inf = 0x3f3f3f3f;18 const int mod = 1e9+7;19 const int maxn = 100000+10;20 void init(){21 22 }23 void solve() {24 char B[30];25 int vis[30], cnt[30], T;26 scanf("%d", &T);27 while(T--){28 scanf("%s", B);29 ms(vis, 0);30 ms(cnt, 0);31 for(int i = 0;i<26;i++){32 if(!vis[i]){33 int j = i, n = 0;34 //cnt为长度35 do{36 vis[j] = 1;37 j = B[j] - 'A';38 n++;39 }while(j!=i);40 cnt[n]++;41 }42 }43 int ok = 1;44 for(int i = 2;i<=26;i+=2)45 if(cnt[i]%2==1)46 ok = 0;47 if(ok) printf("Yes\n");48 else printf("No\n");49 }50 }51 int main() {52 #ifdef LOCAL53 freopen("input.txt", "r", stdin);54 // freopen("output.txt", "w", stdout);55 #endif56 ios::sync_with_stdio(0);57 cin.tie(0);58 init();59 solve();60 return 0;61 }
View Code

 

转载于:https://www.cnblogs.com/denghaiquan/p/7203570.html

你可能感兴趣的文章
加速scp传输速度
查看>>
Kali Linux 安全渗透教程&lt;第三更&gt;1.2 安全渗透所需工具
查看>>
ios 使用Safari浏览器跳转打开、唤醒app
查看>>
HDU 1520 Anniversary party(DFS或树形DP)
查看>>
Linux 安装Nginx具体图解教程
查看>>
Suricata的所有运行方式模式(图文详解)
查看>>
1355: [Baltic2009]Radio Transmission
查看>>
kaldi的TIMIT实例三
查看>>
Prolog 逻辑推导语言
查看>>
又搬回来了233
查看>>
CentOS7下单机部署RabbltMQ环境的操作记录
查看>>
C# 编码命名规则
查看>>
centos7执行 wget命令: command not found的两种解决方法
查看>>
Win8Metro(C#)数字图像处理--2.25二值图像距离变换
查看>>
包管理和环境管理软件Anaconda
查看>>
使用curator 来管理elasticsearch的index
查看>>
manjaro折腾手记
查看>>
vue - webpack.dev.conf.js for merge
查看>>
Jvm(16),jvm创建对象---对象在内存中的创建
查看>>
Microsoft SQL Server 2005 Service fails to start
查看>>