Gym 100625C 密文匹配-模拟题-(map)

题意:已知n个明文和一个密文,推出可能的匹配关系,然后输出字符串ss的密文。

分析:

一个模拟题,当时想偏了,还想着要同一字母可能在任意位置,然后要记录每个字母的位置,找密文的相应位置必须是同一字母,balabala的,不知道什么鬼。

其实就是简单的对应关系,不用管位置啥的,只管同一字母对应的密文是一样的就行了。26个字母,枚举一遍就是了。对应关系匹配啥的用map是最好不过的了。小tirck是,如果已知25个字母,那么剩下的一个也就知道了。

代码:

#include<iostream>
#include<string>
#include<cstring>
#include<map>
#include<algorithm>
using namespace std;
int t,n;
char a[200][1005],b[1005],c[1005];
char ans[200][1005];
map<char,char> mp1,mp2;
int main()
{
	cin>>t;
	while(t--){
		cin>>n;
		for(int i=0;i<n;i++){
			cin>>a[i];
		}
		cin>>b>>c;
		int len1=strlen(b);
		int len2=strlen(c);
		int ok1[10005];
		memset(ok1,0,sizeof(ok1));
		int ok2=0;
		for(int i=0;i<n;i++){
			int l=strlen(a[i]);
			if(l==len1){
				mp1.clear();
				mp2.clear();
				int ok=1,cnt=0;
				for(int j=0;j<len1;j++){
					if(mp1[a[i][j]]==0&&mp2[b[j]]==0){
						mp1[a[i][j]]=b[j];
						cnt++;
						mp2[b[j]]=a[i][j];
					}
					else if(mp1[a[i][j]]==b[j]&&mp2[b[j]]==a[i][j]) continue;
					else{
						ok=0;break;
					}
				}
				if(ok){
					ok1[i]=1;
					ok2=1;
					if(cnt==25){
						char tmp1,tmp2;
						for(char j='a';j<='z';j++){
							if(mp1[j]==0){
								tmp1=j;break;
							}
						}
						for(char j='a';j<='z';j++){
							if(mp2[j]==0){
								tmp2=j;break;
							}
						}
						mp1[tmp1]=tmp2;
					}
					for(int j=0;j<len2;j++){
						if(mp1[c[j]]!=0){
							ans[i][j]=mp1[c[j]];
						}
						else ans[i][j]='?';
					}
				}
			}
		}
		if(!ok2){
			cout<<"IMPOSSIBLE"<<endl;
		}
		else{
			for(int i=0;i<len2;i++){
				char tmp='#';
				for(int j=0;j<n;j++){
					if(ok1[j]){
						if(tmp=='#') tmp=ans[j][i];
						else if(tmp!=ans[j][i]){
							tmp='?';break;
						}
					}
				}
				cout<<tmp;
			}
			cout<<endl;
		}
	}
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-12-25 03:15:51

Gym 100625C 密文匹配-模拟题-(map)的相关文章

HDU 4028 The time of a day STL 模拟题

暴力出奇迹.. #include<stdio.h> #include<iostream> #include<algorithm> #include<vector> #include<cmath> #include<queue> #include<set> #include<map> using namespace std; #define ll __int64 #define N 42 ll n,m,ans;

TOJ1290 Poker Hands 模拟题

寒假期间抽空做的一道模拟题 难度不算大,把每种牌型分开处理,可以合并的步骤考虑合并. 代码比较丑陋,首次尝试Sport Programming的风格,结果搞了个不伦不类(手动笑哭) 1 #include <algorithm> 2 #include <bitset> 3 #include <cctype> 4 #include <complex> 5 #include <cstdio> 6 #include <cstring> 7 #

hdu 5641 King&#39;s Phone(暴力模拟题)

Problem Description In a military parade, the King sees lots of new things, including an Andriod Phone. He becomes interested in the pattern lock screen. The pattern interface is a 3×3 square lattice, the three points in the first line are labeled as

POJ 模拟题集合

http://www.cppblog.com/Uriel/articles/101592.html 感觉这个暑假没有去年有激情啊,,,还没到状态就已经块上学了,,, 真是弱暴了,,,找几道模拟题刷刷... 标加号表示已AC... + 1008   历法,不难 + 1102   不难. + 1028   纯模拟.被题目坑了一下.. 1023   貌似搞了一会儿.. 1051   算是模拟,写得比较麻烦,要细心 1099   跟化学式有关的模拟,有意思,高兴的是这题完全是自己想的AC的.. 1107

cdoj 25 点球大战(penalty) 模拟题

点球大战(penalty) Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/problem/show/25 Description 在足球比赛中,有不少赛事,例如世界杯淘汰赛和欧洲冠军联赛淘汰赛中,当比赛双方经过正规比赛和加时赛之后仍然不分胜负时,需要进行点球大战来决定谁能够获得最终的胜利.点球大战的规则非常简单,两方轮流派出球员罚点球,每方各罚5个.当5轮点球结束以后如果仍然不分胜负,则进入一轮定胜

HDU 4925 Apple Tree(模拟题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4925 解题报告:给你n*m的土地,现在对每一块土地有两种操作,最多只能在每块土地上进行两种操作,第一种是种苹果树操作,第二种是施肥操作,种苹果树操作可以使得该块地 长出一个苹果,施肥操作可以使得与这块土地相邻的土地的苹果产量变为原来的两倍,问可以得到的最多的苹果数量是多少? 例如一个4*4的土地,用1表示在该土地上做第一种操作,0表示在该土地上做第二种操作,可以得到最多苹果的操作如下: 0 1 0

ACdreamoj(1105)模拟题

题意:射一次激光最多可以攻击到几个敌人(由于激光很强大,可以在击中敌人后穿过它,而瑶瑶自己的坦克由于有特殊装置,所以不会被激光击中,激光也会直接穿过它) . 表示此处为空地 * 表示此处为障碍(激光不可穿过,激光路径打到障碍时就结束) T代表瑶瑶的坦克位置 E代表敌人 / 代表按 左下-右上 放置的镜子 \ 代表按 左上-右下 放置的镜子 解法:模拟题.由于位置过多.所以不能用递归模拟,要注意环的判断,还有射击过的敌人要打上标记.wa了n发,最后发现是=写成了==,真的很不应该. 代码: /**

POJ 1008 简单模拟题

e.... 虽然这是一道灰常简单的模拟题.但是米做的时候没有读懂第二个日历的计时方法.然后捏.敲完之后华丽的WA了进一个点.坑点就在一年的最后一天你是该输出本年的.e ...但是我好想并没有..看discuss里好想被坑的人还不少.总天数能直接整除260的时候.年数要减1. 不喜欢做模拟.....5555.... 附代码: #include<stdio.h>#include<string.h>#include<iostream>#include<string>

HDU 4941 Magical Forest _(:зゝ∠)_ 模拟题

模拟大法保平安_(:зゝ∠)_ #include <cstdio> #include <map> #include <set> #include <algorithm> using namespace std; const int N = 1; struct node{ int x, y, val; node(int a=0,int b=0,int c=0):x(a),y(b),val(c){} bool operator<(const node&am