poj3128——置换群(循环)

题目链接:poj.org/problem?id=3128

— 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

题意理解:

给出26个大写字母的置换B,问是否存在一个置换A,使得A^2=B

 

题解:

LRJ大白书上的例题,需要推一下规律。

规律:两个长度为n的相同循环相乘,当n为奇数时结果也是一个长度为n的循环;当n为偶数时分裂成两个长度为n/2的循环。

因此根据题意我们可得:对于任意偶数长度,循环个数为偶数才能配对(26为偶数长度)。

我们只需要求出循环个数就行了。

#include 
#include 
#include 
#include 
#define rp(i, s, t) for (i = s; i <= t; i++)
#define RP(i, s, t) for (i = t; i >= s; i--)
#define ll long long
#define ull unsigned long long
using namespace std;
inline int read()
{
    int x = 0, t = 1;
    char ch = getchar();
    while ((ch < '0' || ch > '9') && ch != '-')
        ch = getchar();
    if (ch == '-')
        t = -1, ch = getchar();
    while (ch <= '9' && ch >= '0')
        x = x * 10 + ch - 48, ch = getchar();
    return x * t;
}
inline void write(int x)
{
    char F[200];
    int tmp = x > 0 ? x : -x;
    if (x < 0)
        putchar('-');
    int cnt = 0;
    while (tmp > 0)
    {
        F[cnt++] = tmp % 10 + '0';
        tmp /= 10;
    }
    while (cnt > 0)
        putchar(F[--cnt]);
}
char s[30];
int visited[30], cnt[30];
int main()
{
    int T = read();
    while (T--)
    {
        scanf("%s", s);
        memset(visited, 0, sizeof(visited));
        memset(cnt, 0, sizeof(cnt));
        int i;
        rp(i, 0, 25)
        {
            if (!visited[i]) //找一个从i开始的循环
            {
                int j = i, n = 0; //n计算循环的长度
                do
                {
                    visited[j] = 1; //标记j为已访问
                    j = s[j] - 'A'; //按照循环往下走
                    n++;
                } while (j != i);
                cnt[n]++; //记录循环的个数
            }
        }
        int flag = 1;
        for (i = 2; i <= 26; i += 2)
            if (cnt[i] & 1) //如果有循环的个数为奇数的话,不能成功配对
                flag = 0;
        if (flag)
            printf("Yes\n");
        else
            printf("No\n");
    }
    return 0;
}

 

 

 

你可能感兴趣的:(组合数学——群论)