CDOJ 1270 Playfair
模拟题,代码写得比较乱。。。
#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
#include<vector>
#include<algorithm>
using namespace std; char s[];
char tmp[];
char u[];
char Map[][];
bool flag[]; vector<int> ans;
int tot; void f()
{
tot = ;
for (int i = ; s[i]; i++){
if (s[i] >= 'a'&&s[i] <= 'z')
tmp[tot++] = s[i];
else if (s[i] >= 'A'&&s[i] <= 'Z')
tmp[tot++] = s[i] - 'A' + 'a';
} tmp[tot] = ;
for (int i = ; tmp[i]; i++)
if (tmp[i] == 'j') tmp[i] = 'i';
} void EYET()
{
for (int i = ; s[i]; i++){
if (s[i] >= 'a'&&s[i] <= 'z')
tmp[tot++] = s[i];
else if (s[i] >= 'A'&&s[i] <= 'Z')
tmp[tot++] = s[i] - 'A' + 'a';
} tmp[tot] = ;
for (int i = ; tmp[i]; i++)
if (tmp[i] == 'j') tmp[i] = 'i';
} void Fill()
{
tot = ;
memset(Map, , sizeof Map);
memset(flag, , sizeof flag);
for (int i = ; tmp[i]; i++)
{
// printf("%c %d \n", tmp[i], flag[tmp[i] - 'a']);
if (flag[tmp[i] - 'a'] == )
{
flag[tmp[i] - 'a'] = ; tot++;
int r, c;
if (tot % == ) r = tot / ;
else r = tot / + ;
c = tot % ;
if (c == ) c = ;
Map[r][c] = tmp[i];
}
} for (int i = ; i<; i++)
{
char sign = i + 'a';
if (sign == 'j') continue;
if (flag[i] == ) continue; tot++;
int r, c;
if (tot % == ) r = tot / ;
else r = tot / + ;
c = tot % ;
if (c == ) c = ;
Map[r][c] = sign;
}
} bool P1(char a, char b)
{ int posxA, posyA; int posxB, posyB;
for (int i = ; i <= ; i++)
{
for (int j = ; j <= ; j++)
{
if (Map[i][j] == a) posxA = i, posyA = j;
if (Map[i][j] == b) posxB = i, posyB = j;
}
} if (posxA == posxB)
{
posyA++;
if (posyA > ) posyA = ; posyB++;
if (posyB > ) posyB = ; ans.push_back(Map[posxA][posyA] - 'a');
ans.push_back(Map[posxB][posyB] - 'a'); return ;
}
return ;
} bool P2(char a, char b)
{ int posxA, posyA; int posxB, posyB;
for (int i = ; i <= ; i++)
{
for (int j = ; j <= ; j++)
{
if (Map[i][j] == a) posxA = i, posyA = j;
if (Map[i][j] == b) posxB = i, posyB = j;
}
} if (posyA == posyB)
{
posxA++;
if (posxA > ) posxA = ; posxB++;
if (posxB > ) posxB = ; ans.push_back(Map[posxA][posyA] - 'a');
ans.push_back(Map[posxB][posyB] - 'a'); return ;
}
return ;
} void P3(char a, char b)
{
int posxA, posyA; int posxB, posyB; for (int i = ; i <= ; i++)
{
for (int j = ; j <= ; j++)
{
if (Map[i][j] == a) { posxA = i, posyA = j; }
if (Map[i][j] == b) { posxB = i, posyB = j; }
}
} ans.push_back(Map[posxA][posyB] - 'a');
ans.push_back(Map[posxB][posyA] - 'a');
} void work()
{
int len = strlen(tmp);
strcpy(u, tmp); queue<int>Q;
for (int i = ; i < len; i++) Q.push(tmp[i]-'a'); while (!Q.empty())
{
char sign1 = Q.front() + 'a';
char sign2;
Q.pop();
if (Q.empty()) break;
if (Q.front()+'a' == sign1)
{
sign2 = 'x';
if (P1(sign1, sign2) == ) continue;
else if (P2(sign1, sign2) == ) continue;
else P3(sign1, sign2);
}
else
{
sign2 = Q.front() + 'a';
Q.pop();
if (P1(sign1, sign2) == ) continue;
else if (P2(sign1, sign2) == ) continue;
else P3(sign1, sign2);
}
}
} int main()
{
while (gets(s))
{
ans.clear();
f();
Fill(); bool Exit = ; tot = ;
while ()
{
scanf("%s", s);
int len = strlen(s);
if (s[] == '*' || s[len - ] == '*') Exit = ;
EYET();
if (Exit) break;
}tmp[tot] = ;
work();
for (int i = ; i < ans.size(); i++) printf("%c", ans[i] + 'a');
printf("\n");getchar();
}
return ;
}
CDOJ 1270 Playfair的更多相关文章
- CDOJ 1270 Playfair(模拟)
题目链接 Playfair is a kind of substitution cipher.And the encryption role is simple.In general,there ar ...
- 信息安全-1:python之playfair密码算法详解[原创]
转发注明出处: http://www.cnblogs.com/0zcl/p/6105825.html 一.基本概念 古典密码是基于字符替换的密码.加密技术有:Caesar(恺撒)密码.Vigenere ...
- poj 1270(toposort)
http://poj.org/problem?id=1270 题意:给一个字符串,然后再给你一些规则,要你把所有的情况都按照字典序进行输出. 思路:很明显这肯定要用到拓扑排序,当然看到discuss里 ...
- cdoj 1489 老司机采花
地址:http://acm.uestc.edu.cn/#/problem/show/1489 题目: 老司机采花 Time Limit: 3000/1000MS (Java/Others) M ...
- hiho #1361 Playfair密码表
题目1 : Playfair密码表 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho经常用Playfair密码表加密自己的代码. 密码表是按以下步骤生成的. ...
- python playfair
#########################Playfair密码######################### #约定1:若明文字母数量为奇数,在明文末尾添加一个'Z' #约定2:'I'作为 ...
- 【BZOJ】1270: [BeijingWc2008]雷涛的小猫(DP+水题)
http://www.lydsy.com/JudgeOnline/problem.php?id=1270 这完全是一眼题啊,但是n^2的时间挺感人.(n^2一下的级别请大神们赐教,我还没学多少dp优化 ...
- hdu 1270 小希的数表
思路:一定有sum[1]=num[1]+num[2],sum[2]=num[1]+num[3]; 但是sum[3]不知道是由num[1]+num[4]还是num[2]+num[3],这就需要枚举一下了 ...
- BZOJ 1270: [BeijingWc2008]雷涛的小猫( dp )
简单的dp.. dp(i,j) = max(dp(x,y))+cnt[i][j], (x,y)->(i,j)是合法路径. 设f(i)= max(dp(x,y))(1≤x≤N, 1≤y≤i), g ...
随机推荐
- C++字符串(1)
C++ 拼接字符串常量 C++允许拼接字符串字面值,即将两个用引号括起的字符串合并为一个.事实上,任何两个由空白(空格,制表符和换行符)分隔的字符串常量都将自动拼接成一个. 例子: cout < ...
- 文字在边界自动换行word-wrap:break-word
div容器内中内容将在边界内换行,(word-wrap)英语句子中单词内不强制换行.(word-break)如果需要词内换行
- sql 指定范围 获取随机数
DECLARE @nMinimumCount INT= 1DECLARE @nMaximumCount INT= 100SELECT abs(CHECKSUM(NEWID()))%(@nMaximum ...
- 7--OC中NSLog函数输出格式详解
OC中NSLog函数输出格式详解 • %@ 对象 • %d, %i 整数 • %u 无符整形 • %f 浮点/双字 • %x, %X 二进制整数 • %o 八进制整数 • %zu size_t • % ...
- 学习笔记——门面模式Facade
门面模式,其实在我们不经意间已经使用了此设计模式.当我们需要将两个子系统,合并对外提供一个大的接口时,我们使用的就是门面模式.对外,子系统的接口是不可见的,只有我们的门面在.
- System services not available to Activities before onCreate()
应用中涉及到系统的mac地址获取,应该是不能够在oncreate()以前使用
- Activity not started, its current task has been brought to the front的解决办法
删除bin目录下所有文件,重新启动在试试
- asp读取指定目录下的文件名
bianli(Server.MapPath("/")+"\pic") InStrRev("abcd.jpg", ".") ...
- Centos6.5安装与配置Tomcat-8的方法
环境要求: 系统: [root@Wulaoer ~]# cat /proc/version Linux version 2.6.32-431.el6.x86_64 (mockbuild@c6b8.bs ...
- Ubuntu将新增磁盘挂载到home下
home磁盘空间不足,其他闲置硬盘是原来windows的,不能直接使用(磁盘格式及权限等原因),比如编译安卓源码等. 这样的话就需要将新的磁盘格式化成fat32后挂载到/home下的一个目录,这样就可 ...