1. 程式人生 > >POJ 3128 Leonardo's Notebook (置換)

POJ 3128 Leonardo's Notebook (置換)

gif telling freopen for each align lock 需要 text page

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 (置換)