码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 3128 Leonardo's Notebook (置换)

时间:2017-07-19 01:09:29      阅读:255      评论:0      收藏:0      [点我收藏+]

标签:奇数   ordinary   abc   clu   题意   queue   tac   idt   computer   

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

2
QWERTYUIOPASDFGHJKLZXCVBNM
ABCDEFGHIJKLMNOPQRSTUVWXYZ

Sample Output

No
Yes

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 <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <string>
 5 #include <algorithm>
 6 #include <cmath>
 7 #include <vector>
 8 #include <queue>
 9 #include <stack>
10 #include <set>
11 using namespace std;
12 typedef long long LL;
13 #define ms(a, b) memset(a, b, sizeof(a))
14 #define pb push_back
15 #define mp make_pair
16 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 LOCAL
53     freopen("input.txt", "r", stdin);
54 //        freopen("output.txt", "w", stdout);
55 #endif
56     ios::sync_with_stdio(0);
57     cin.tie(0);
58     init();
59     solve();
60     return 0;
61 }
View Code

 

POJ 3128 Leonardo's Notebook (置换)

标签:奇数   ordinary   abc   clu   题意   queue   tac   idt   computer   

原文地址:http://www.cnblogs.com/denghaiquan/p/7203570.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!