poj 3128 Leonardo's Notebook (置换)

Leonardo's Notebook
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 2158 Accepted: 956

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

[Submit]   [Go Back]   [Status]   [Discuss]


题目大意:给出一个置换,判断当前置换能不能写成一个置换的平方的形式(就是一个置换做两次的效果叠加)

题解:置换

这个题其实是可以规律的。

我们把置换分解成几个轮换相乘的形式,那么一个置换做两次对于每个轮换的影响是元素个数为奇数的轮换不变,元素个数为偶数的轮换会平均分裂成两个。例(1,3,6)(2,5)(4)两次的效果会变成(1,3,6)(2)(4)(5).

那么如果现在给出的轮换种元素个数为某个偶数的轮换个数为奇数个的话,一定不能写成某个置换平方的形式。因为如果当前是轮换的元素个数为偶数,那么他一定是由某个轮换分裂成的,那么一定是成对出现的。如果他最初没平方的时候是偶数,一定会发生分裂。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define N 100
using namespace std;
int n,cnt[N],a[N],use[N],num[N];
char s[N];
int main()
{
	freopen("a.in","r",stdin);
	scanf("%d",&n); int m=26;
	for (int i=1;i<=m;i++) cnt[i]=i; 
	while (n--) {
		scanf("%s",s+1);
		memset(use,0,sizeof(use));
		memset(num,0,sizeof(num));
		for (int i=1;i<=m;i++) a[i]=s[i]-'A'+1;
		int len=0;
		for (int i=1;i<=m;i++) 
		 if (!use[i]) {
		 	int j=i; int len=0;
		 	while (!use[j]) {
		 		len++;
		 		use[j]=1; j=cnt[a[j]];
			 }
			num[len]++;
		 }
		bool pd=true;
		for (int i=1;i<=m;i++)
		 if (i%2==0&&num[i]&1) pd=false;
		if (!pd) printf("No\n");
		else printf("Yes\n");
	}
} 



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值