pku acm 1035

//Spell checker

#include <iostream>
#include <cstdio>
#include <string>
#include <set>
using namespace std;

set<string> dic;
string dictxt[10005];
int index = 0;

bool isInDic(string& s)
{
	set<string>::iterator it;
	it = dic.find(s);
	if (it != dic.end())
		return true;
	return false;
}

void search(string& str)
{
	if(isInDic(str))
	{
		cout<<str<<" is correct"<<endl;
		return;
	}
	cout<<str<<":";

	int i,j;
	int lens = str.length(),
		lend;
	string s;
	for (i = 0; i < index; ++i)
	{		
		lend = dictxt[i].length();

		switch(lens-lend)
		{
		case 0://replace
			s = str;
			for (j = 0; j < lend; ++j)
				if (dictxt[i][j] != s[j])
				{
					s[j] = dictxt[i][j];
					break;
				}
			if(s == dictxt[i])			
				cout<<" "<<dictxt[i];			
			break;

		case -1://insert
			s = str;
			for (j = 0; j < lens; ++j)
				if (dictxt[i][j] != s[j])
				{
					s.insert(j,1,dictxt[i][j]);
					break;
				}
			if(j == lens || s == dictxt[i])				
				cout<<" "<<dictxt[i];				
			break;

		case 1://delete
			s = str;
			for (j = 0; j < lend; ++j)
				if (dictxt[i][j] != s[j])
				{
					s.erase(j,1);
					break;
				}
			if (j == lend || s == dictxt[i])			
				cout<<" "<<dictxt[i];					
			break;
		}

	}

	cout<<endl;
}

int main()
{
	freopen("in.txt","r",stdin);

	index = 0;
	while (cin>>dictxt[index] && dictxt[index] != "#")
	{
		dic.insert(dictxt[index]);
		++index;
	}

	string s;
	while (cin>>s && s != "#")
		search(s);	
		
}

測試數據:


aaa
aab
#
aaa
aa
aaaa
aba
baa
aab
bba
abb
bab
#

i
is
has
have
be
my
more
contest
me
too
if
award
#
me
aware
m
contest
hav
oo
or
i
fi
mre
#

第一組測試數據很重要,注意邊界情況的處理。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章