POJ 3128 Leonardo's Notebook (置换)
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 2324 | Accepted: 988 |
Description

— 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
Output
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。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <set>
using namespace std;
typedef long long LL;
#define ms(a, b) memset(a, b, sizeof(a))
#define pb push_back
#define mp make_pair
const int INF = 0x7fffffff;
const int inf = 0x3f3f3f3f;
const int mod = 1e9+;
const int maxn = +;
void init(){ }
void solve() {
char B[];
int vis[], cnt[], T;
scanf("%d", &T);
while(T--){
scanf("%s", B);
ms(vis, );
ms(cnt, );
for(int i = ;i<;i++){
if(!vis[i]){
int j = i, n = ;
//cnt为长度
do{
vis[j] = ;
j = B[j] - 'A';
n++;
}while(j!=i);
cnt[n]++;
}
}
int ok = ;
for(int i = ;i<=;i+=)
if(cnt[i]%==)
ok = ;
if(ok) printf("Yes\n");
else printf("No\n");
}
}
int main() {
#ifdef LOCAL
freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
ios::sync_with_stdio();
cin.tie();
init();
solve();
return ;
}
POJ 3128 Leonardo's Notebook (置换)的更多相关文章
- poj 3128 Leonardo's Notebook——思路(置换)
题目:http://poj.org/problem?id=3128 从环的角度考虑. 原来有奇数个点的环,现在点数不变: 原来有偶数个点的环(设有 k 个点),现在变成两个大小为 k/2 的环. 所以 ...
- poj 3128 Leonardo's Notebook (置换群的整幂运算)
题意:给你一个置换P,问是否存在一个置换M,使M^2=P 思路:资料参考 <置换群快速幂运算研究与探讨> https://wenku.baidu.com/view/0bff6b1c6bd9 ...
- POJ 3128 Leonardo's Notebook [置换群]
传送门 题意:26个大写字母的置换$B$,是否存在置换$A$满足$A^2=B$ $A^2$,就是在循环中一下子走两步 容易发现,长度$n$为奇数的循环走两步还是$n$次回到原点 $n$为偶数的话是$\ ...
- poj 3128 Leonardo's Notebook(置换的幂)
http://poj.org/problem?id=3128 大致题意:输入一串含26个大写字母的字符串,能够把它看做一个置换.推断这个置换是否是某个置换的平方. 思路:具体解释可參考url=ihxG ...
- UVaLive 3641 Leonardo's Notebook (置换)
题意:给定一个置换 B 问是否则存在一个置换 A ,使用 A^2 = B. 析:可以自己画一画,假设 A = (a1, a2, a3)(b1, b2, b3, b4),那么 A^2 = (a1, a2 ...
- UVA12103 —— Leonardo's Notebook —— 置换分解
题目链接:https://vjudge.net/problem/UVA-12103 题意: 给出大写字母“ABCD……Z”的一个置换B,问是否存在一个置换A,使得A^2 = B. 题解: 对于置换,有 ...
- [Poj3128]Leonardo's Notebook
[Poj3128]Leonardo's Notebook 标签: 置换 题目链接 题意 给你一个置换\(B\),让你判断是否有一个置换\(A\)使得\(B=A^2\). 题解 置换可以写成循环的形式, ...
- LA 3641 (置换 循环的分解) Leonardo's Notebook
给出一个26个大写字母的置换B,是否存在A2 = B 每个置换可以看做若干个循环的乘积.我们可以把这些循环看成中UVa 10294的项链, 循环中的数就相当于项链中的珠子. A2就相当于将项链旋转了两 ...
- Leonardo's Notebook UVALive - 3641(置换)
题意: 给出26个大写字母的置换B,问是否存在一个置换A,使得A2 = B 解析: 两个长度为n的相同循环相乘,1.当n为奇数时结果也是一个长度为n的循环:2. 当n为偶数时分裂为两个长度为n/2 ( ...
随机推荐
- vue项目 时间戳转 格式
项目用了 element UI的日期插件,修改时 时间回显不了,打印出来是换行了,因此要转换 changeTime(value){ let date = new Date(value); let y ...
- ChinaCock打印控件介绍-TCCFujitsuPrinter实现蓝牙针式打印
项目中遇到,要蓝牙针式打印机,用手机打印表单.感谢专家,对厂家提供的SDK进行了封装,实现利用Delphi开发出这一功能. 现在来看看,如何利用这一控件实现打印过程: procedure startS ...
- java调用ffmpeg获取视频文件信息的一些参数
一.下载ffmpeg http://www.ffmpeg.org/download.html 主要需要bin目录下的ffmpeg可执行文件 二.java代码实现 package com.aw.util ...
- 关于PXELINUX的一些重要描述摘录
以下资源都来自官方文档,原文摘录 PXELINUX is a SYSLINUX derivative, for booting Linux off a network server, using a ...
- 第十一章· MHA高可用及读写分离
一.MHA简介 1.1.作者简介 松信嘉範: MySQL/Linux专家 2001年索尼公司入职 2001年开始使用oracle 2004年开始使用MySQL 2006年9月-2010年8月MySQL ...
- UNetbootin安装linux
用u盘安装linux系统,最好的方法莫过于用UNetbootin,网址:http://unetbootin.github.io/ UNetbootin allows you to create boo ...
- deep_learning_Function_tf.argmax()解析
tf.argmax(input,axis)根据axis取值的不同返回每行或者每列最大值的索引. 这个很好理解,只是tf.argmax()的参数让人有些迷惑,比如,tf.argmax(array, 1) ...
- three.js之元素周期表
<html><head> <title>three.js css3d - periodic table</title> <meta charset ...
- c++四种分配内存的方法整理
1 calloc 函数: void *calloc(unsigned int num, unsigned int size) 按照所给的数据个数和数据类型所占字节数,分配一个 num * size 连 ...
- IDEA利用mybatis-generator自动生成dao和mapper
pom.xml配置 <properties> <java.version>1.8</java.version> <mybatis-generator-core ...