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 ...
随机推荐
- rebar
www.cnblogs.com/panfeng412/archive/2011/08/14/2137990.html
- byte数组与int,long,short,byte转换 (转载)
byte数组和short数组转换 public short bytesToShort(byte[] bytes) { return ByteBuffer.wrap(bytes).order(ByteO ...
- asp之vbscript函数
'函数Abs(number)'返回绝对值.Array(arglist)'创建一个数组.Asc(string)'返回字符串第一个字符的ANSI码.Atn(number)'返回反正弦值.CBool(exp ...
- 网站网址前的小logo
认识网页前小图标 1.能在浏览器标签.地址栏左边和收藏夹栏显示小图标的网站,其网站都是使用了其名称为"favicon.ico"图标文件,格式为ico格式,图标大小一般为16*16, ...
- 解决oracle数据库连接不上的问题
今天打开部署好的java开发的网站系统,反应好慢,第一反应就是后台有问题. 查看tomcat一堆的报错信息,重启还是存在. 使用plSql连接数据库看看,登录提示如下:ORA-12514:TNS:监听 ...
- 为ubuntu添加右键打开终端效果
安装工具:nautilus-open-terminal sudo apt-get install nautilus-open-terminal 注销或重启
- C语言根据函数名调用对应的函数
通过函数指针定义,调用时加上参数 struct Command { const char *name; const char *desc; // return -1 to force monitor ...
- Setting DPDK+OVS+QEMU on CentOS
Environment Build Step: these packages are needed for building dpdk+ovs: yum install -y make gcc gli ...
- HDU1492/The number of divisors(约数) about Humble Numbers
题目连接 The number of divisors(约数) about Humble Numbers Time Limit: 2000/1000 MS (Java/Others) Memory L ...
- mysql 初始化时root无密码
修改密码 update user set password=PASSWORD('123456') where User='root'; 添加用户设置权限 grant select,insert,upd ...