习题4_6 uva508 莫尔斯电码

事实证明看不懂英文的代价是惨痛的=_=// 可能你会一直哇 一时wa题一时爽,一直wa题一直爽 o_o
题目意思:
匹配 : 若有一个完全匹配,则输出该单词,若有多个,则输出 字典序最小的单词 + “!”
else 找模糊匹配,找到增删最少的,输出,若有多个,同上,输出字典序最小的单词
不管几个 都加 “?”
else 输出 字典中字典序最小的单词 + “?”
(单组输入emmm)
好像…看懂了题目就像…
罢了上代码

#include <bits/stdc++.h>
using namespace std;
map<char,string> mor; //字符对应莫尔斯 
map<string,string> word;//莫尔斯对应的最小字典序的单词
map<string,int> ge;//莫尔斯可对应单词个数 
string minword,mset[2000];
int m;
int pan(string a, string b)//判断是否不是字典序最小 
{
	return a > b;
}
void match(string mo)//模糊匹配 
{
	int n = mo.size(),biao = -1,jian = 10000;
	for (int i = 0; i < m; i++)
	{
		int len = mset[i].size(),hh = min(n,len);
		int cha = abs(len-n),ok = 1;
		for (int j = 0; j < hh ; j++)
		{
			if(mo[j] != mset[i][j]) 
			{
				ok = 0; break;
			}
		}
		if(ok)
		{
			if(jian > cha)
			{
				jian = cha; biao = i;
			}
			else if(jian == cha && pan(mset[biao],mset[i])) biao = i;
		}
	}
	if(biao == -1) cout<<minword<<"?"<<endl;//没有
	else  cout<<word[mset[biao]]<<"?"<<endl; 
}
int main()
{
	string c,mo;
	while(cin>>c && c!="*")
	{
		cin>>mo;
		mor[c[0]] = mo;
	}
	while(cin>>c && c!="*")
	{
		if(minword == "") minword = c;
		else if(pan(minword,c)) minword = c;
		//求最小字典序单词 
		mo = "";
		int n = c.size();
		for (int i = 0; i < n;i++)
		   mo += mor[c[i]];
		if(!ge[mo] || pan(word[mo],c))  word[mo] = c;
		if(!ge[mo]) mset[m++] = mo;
		   ge[mo]++;//对每个莫尔斯计数		
	}
	while(cin>>c && c!="*")
	{
		if(!ge[c]) match(c); 
		else  cout<<word[c]<<((ge[c] == 1)?"":"!")<<endl;
	} 
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值