Exercise 4_6 uva508 Morse code

Facts have proved not read English at the cost of painful = _ = // likely you will have problems sometimes cool wow moment wa, wa problem has been cool o_o
title means:
match: if a fully match the output of that word, if there are more, then the output lexicographically smallest word + "!"
the else fuzzy matching to find to find the minimal additions and deletions, output, if more than one, above, output lexicographically smallest word
regardless of a few have added "?"
the else output dictionary lexicographically smallest word + "?"
(a single set of input emmm)
like ... like ... read the subject
fills the Code

#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;
}
Published 55 original articles · won praise 1 · views 2667

Guess you like

Origin blog.csdn.net/qq_37548017/article/details/99625462