poj 1035 Spell checker

http://poj.org/problem?id=1035

题意:给出一些字典单词,然后给出查询的单词temp,若字典单词中有temp就直接输出,如果没有就输出1:一个字母拼写错误;2:多添加了 一个字母;3:少了一个字母;

思路:用vector存的string序列,然后用string的一些特性来判断的;

代码:

View Code
#include <cstdio>
#include <string>
#include <cstring>
#include <iostream>
#include <cstdlib>
#include <vector>
using namespace std;
vector<string>vec;
string temp;
int biao = 0;
void work(string a,string b)
{
int len = a.length();
int count = 0;
for(int i = 0;i < len; ++i)
{
if(a[i] != b[i])
count++;
if(count > 1)
return ;
}
cout<<" "+a;
}
void work1(string a,string b,int q)
{
int len = 0;
len = b.length();
for(int i = 0;i < len; ++i)
{
if(a[i] != b[i])
{
string temp = a;
temp.erase(i,1);
if(b == temp)
{
if(q)
cout<<" "+a;
else
cout<<" "+b;
}
return ;
}
}
if(q)
cout<<" "+a;
else
cout<<" "+b;
}
int main()
{
//freopen("input.txt","r",stdin);
while(1)
{
cin>>temp;
if(temp == "#")
break;
vec.push_back(temp);
}

while(1)
{
int biao = 0;
cin>>temp;
if(temp == "#")
break;
int len1 = vec.size();
int len = temp.length();
for(int i = 0;i < len1;++i)
if(vec[i] == temp)
{
biao = 1;
cout<<temp+" is correct"<<endl;
break;
}
if(biao)
continue;
cout<<temp+":";
int le = 0;
for(int i = 0;i < len1; ++i)
{
le = vec[i].length();
if(le == len)
work(vec[i],temp);
else
if(le == len +1)
work1(vec[i],temp,1);
else
if(len == le + 1)
work1(temp,vec[i],0);
}
cout<<endl;
}
return 0;
}



posted @ 2012-03-25 16:07  LT-blogs  阅读(253)  评论(0编辑  收藏  举报