UVALive 6555 Playing Fair with Cryptography(细节处理)
2016-03-24 17:11
369 查看
题目链接:
UVALive 6555 Playing Fair with Cryptography
题意:
先给出一行字符,可能有空格,标点符号,数字,大小写英文字母等,按照英文字母出现的顺序建立一个5行5列的大写英文字母表格,
(I和J占同一格子,且输入保证不会有J或j)然后第二行也是同样的输出,对于每相邻两个英文字母按照题目中的要求在表中找到相应的英文字母输出。
分析:
一定要仔细读题,全是细节!
UVALive 6555 Playing Fair with Cryptography
题意:
先给出一行字符,可能有空格,标点符号,数字,大小写英文字母等,按照英文字母出现的顺序建立一个5行5列的大写英文字母表格,
(I和J占同一格子,且输入保证不会有J或j)然后第二行也是同样的输出,对于每相邻两个英文字母按照题目中的要求在表中找到相应的英文字母输出。
分析:
一定要仔细读题,全是细节!
#include <iostream> #include <cstdio> #include <cmath> #include <cstring> #include <ctype.h> #include <algorithm> #include <stdlib.h> using namespace std; int T,cases=0; char c,cc,mapp[10][10]; int vis[30],col[30],row[30],vvis[30][30]; void fun(char a,char b) { int ra=row[a-'A'],ca=col[a-'A']; int rb=row[b-'A'],cb=col[b-'A']; if(ra==rb){ printf("%c%c",mapp[ra][(ca+1)%5],mapp[rb][(cb+1)%5]); }else if(ca==cb){ printf("%c%c",mapp[(ra+1)%5][ca],mapp[(rb+1)%5][cb]); }else{ printf("%c%c",mapp[ra][cb],mapp[rb][ca]); } } int calc(int i) { int t=i; t=(t+1)%26; if(t==9) t++;//A相当于0,那么J相当于9,如果是9就再跳过 return t; } int main() { freopen("in.txt","r",stdin); scanf("%d",&T); while(T--){ memset(vis,0,sizeof(vis)); vis[9]=1; memset(vvis,0,sizeof(vvis)); int read_end=0,first=1; for(int i=0;i<5;i++){ for(int j=0;j<5;j++){ while(1){ scanf("%c",&c); if(first==1){//读到的第一个字符是上一行的换行符 first=0; if(c=='\n') continue; } if(isalpha(c)){//直到读到没被用过的英文字母或者换行符时才退出 c=toupper(c); if(!vis[c-'A']) break; } else if(c=='\n') break; } if(c=='\n'){ read_end=1; break; } vis[c-'A']=1,row[c-'A']=i,col[c-'A']=j;//记录字母已经出现和字母所在行列 mapp[i][j]=c; vvis[i][j]=1; } if(read_end==1) break; } int ind=0; for(int i=0;i<5;i++){ for(int j=0;j<5;j++){ if(vvis[i][j]==1) continue;//已经有过字母了,就跳过 while(vis[ind]){//依次找到没被用过的字母 ind++; } mapp[i][j]='A'+ind; row[ind]=i,col[ind]=j;//记录字母行列 ind++; } } ind=0; printf("Case %d: ",++cases); int flag=1; first=1; while(1){ if(flag==1){//用于判断是否读入两个相同的字母,如果相同,那么下次只要读入一个字母 while(1){ scanf("%c",&c); if(first==1){ first=0; if(c=='\n') continue;//读到的第一个字符是上一行的换行符 } if(isalpha(c)||c=='\n') break;//直到读到英文字母或者本行结束跳出读入 } if(c=='\n') { printf("\n"); break; } c=toupper(c); } while(1){//读第二个字母 scanf("%c",&cc); if(isalpha(cc)||cc=='\n') break; } if(cc=='\n'){//第二个字母读到换行符但是仍需处理读到的第一个字母 if(c=='A'+ind) ind=calc(ind); fun(c,'A'+ind); printf("\n"); break; } cc=toupper(cc); if(cc==c){//第一个字母和第二个字母一样 if(c=='A'+ind) ind=calc(ind);//第一个字母和替代字母一样 fun(c,'A'+ind); ind=(ind+1)%26;//已经用过的替代字母就不能再用了!这里我WA了6发! c=cc; flag=0;//下次就不用读第一个字母了 }else { flag=1;//两个字母不一样 fun(c,cc); } } } return 0; }
相关文章推荐
- UVALive 4244 Party Party Party(HDU 2779 && Sicily 1663)
- UVALive 6741 The Sacrificial Firepits
- UVALive 5870 - Smooth Visualization
- UVALive 5876 - Writings on the Wall 【KMP】
- UVALive 5878 - Shortest Leash 【计算几何】
- UVALive 5873 - Tree Inspections 【模拟】
- UVALive 6800 The Mountain of Gold (bellman_ford判负环)
- UVALive 6436 The Busiest City
- Uvalive 6855 Banks
- Uvalive 7150 Amalgamated Artichokes
- Uvalive 4954 Lawn mower
- Uvalive 6832 Bit String Reordering
- Uvalive 6398 Low Power
- Uvalive 6929 Sums
- UVALive 5913 【NEERC 2011 K】 Kingdom Roadmap
- UVALive 6886
- UVALive 7146 Defeat the Enemy (贪心)
- UVALive 6582 Magical GCD
- uvalive 5873
- UVALive 5135 - Mining Your Own Business(点双连通)