POJ 3128 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
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 ( ...
随机推荐
- 03 Go语言特性
一.基本注意事项 1.转义字符 \t 一个制表符,代表一次tab \n 换行符 \\ 转义代表 \ \" 转义代表 " \r 一个回车,从当前行的最前面开始输出,会覆盖以前的内容, ...
- URL的 ? 和 # (hash),如何将参数保存在URL中,用于刷新获取之前的变量?
URL中会带上参数,假如是?开头的,那这个是会被加入到ajax请求中的,#(hash)相当于书签锚点,用于定位页面,不会加入到ajax请求中,所以有些时候,我们可以把一些参数放在#后面 如何获取URL ...
- PHP获取某段文字作为标题
<?php mb_internal_encoding('utf-8'); // 提取文字标题,多余文字用省略号替换 $arr=[ '用心用情用功,进行无愧于时代的文艺创造', '一图了解第二届一 ...
- XSS防御和绕过2
上一篇已经总结过,这里转载一篇,备忘 0x01 常规插入及其绕过 转自https://blog.csdn.net/qq_29277155/article/details/51320064 1 Scri ...
- Wxpython pannel切换
演示效果 实现panel切换思路 1.创建所有在某个区域需要切换面板对象,设置为None self.panel_Celan1 = None self.panel_Celan2 = None self. ...
- php实现雪花算法(ID递增)
雪花算法简单描述: 最高位是符号位,始终为0,不可用. 41位的时间序列,精确到毫秒级,41位的长度可以使用69年.时间位还有一个很重要的作用是可以根据时间进行排序. 10位的机器标识,10位的长度最 ...
- MySQL 数据类型简介 创建数据表及其字段约束
数据类型介绍 MySQL 数据类型分类 整型 浮点型 字符类型(char与varchar) 日期类型 枚举与集合 具体数据类型见这篇博客 MySQL表操作中的约束 primary key 主键约束 非 ...
- service worker 实现页面通信
sw.js 基本写法: function send_message_to_sw(msg){ navigator.serviceWorker.controller.postMessage("C ...
- hive-staging文件产生的原因和解决方案
通过spark-sql.hive-sql.hue等提交select或者insert overwrite等sql到hive时,会产生该目录,用于临时存放执行结果,比如insert overwrite会将 ...
- YII2.0.12兼容PHP7.2版本升级
YII2.0.12兼容PHP7.2版本升级 报错信息: FastCGI sent in stderr: "PHP message: PHP Fatal error: Cannot use ...