您的位置:首页 > 其它

【原】 POJ 1035 Spell checker 编辑距离 解题报告

2010-11-04 21:12 399 查看
http://poj.org/problem?id=1035

 

方法一:动态规划 O(len^2)

EditDist

c[i,j]表示s1[0...i]和s2[0...j]的编辑距离,由于矩阵c的第一行和第一列为基准情况,所以实际中c[len1,len2]为s1和s2的编辑距离
当s1[i]==s2[j]时,自然而然的c[i,j] = c[i-1][j-1]
当s1[i]!=s2[j]时,先按照变动s1来讨论:
      删除s1[i],再观察s1[0...i-1]和s2[0...j]的编辑距离,为c[i-1,j]
      将s1[i]替换成s2[j],此时s1[i]==s2[j],再观察s1[0...i-1]和s2[0...j-1]的编辑距离,为c[i-1,j-1]
      在s1[i]后添加s[j],再观察s1[0...i]和s2[0...j-1]的编辑距离,为c[i,j-1]
此时c[i,j]即为c[i-1,j]、c[i,j-1]、c[i-1,j-1]三者中的最小值再加上一次操作
对于s2[j]的这三种操作所获得的需要计算的矩阵c中的项是相同的
因此可得如下递归式

c[i,j] = c[i-1][j-1] , if s1[i]==s2[j]
c[i,j] = min{ c[i][j-1] , c[i-1][j] , c[i-1][j-1]}+1 , otherwise

 

方法二:顺序扫描 O(len)

run1035()

由于只求编辑距离为1,因此可以得到线性算法

 

Description

You, as a member of a development team for a new spell checking program, are to write a module that will check the correctness of given words using a known dictionary of all correct words in all their forms.
If the word is absent in the dictionary then it can be replaced by correct words (from the dictionary) that can be obtained by one of the following operations:
?deleting of one letter from the word;
?replacing of one letter in the word with an arbitrary letter;
?inserting of one arbitrary letter into the word.
Your task is to write the program that will find all possible replacements from the dictionary for every given word.

Input

The first part of the input file contains all words from the dictionary. Each word occupies its own line. This part is finished by the single character '#' on a separate line. All words are different. There will be at most 10000 words in the dictionary.
The next part of the file contains all words that are to be checked. Each word occupies its own line. This part is also finished by the single character '#' on a separate line. There will be at most 50 words that are to be checked.
All words in the input file (words from the dictionary and words to be checked) consist only of small alphabetic characters and each one contains 15 characters at most.

Output

Write to the output file exactly one line for every checked word in the order of their appearance in the second part of the input file. If the word is correct (i.e. it exists in the dictionary) write the message: " is correct". If the word is not correct then write this word first, then write the character ':' (colon), and after a single space write all its possible replacements, separated by spaces. The replacements should be written in the order of their appearance in the dictionary (in the first part of the input file). If there are no replacements for this word then the line feed should immediately follow the colon.

Sample Input

i

is

has

have

be

my

more

contest

me

too

if

award

#

me

aware

m

contest

hav

oo

or

i

fi

mre

#

Sample Output

me is correct

aware: award

m: i my me

contest is correct

hav: has have

oo: too

or:

i is correct

fi: i

mre: more me

 

 

[code]     [code] int EditDist(const string& s1,const string& s2,int c[15+1][15+1])


{


int len1 = s1.size() ;


int len2 = s2.size() ;


int i,j ;


int strIndex1,strIndex2 ;


int d1,d2,d3 ;


int min ;


 


for( i=1 ; i<len1+1 ; ++i )


  {


for( j=1 ; j<len2+1 ; ++j )


 {


strIndex1 = i-1 ;


strIndex2 = j-1 ;


 


if( s1[strIndex1] == s2[strIndex2] )


c[i][j] = c[i-1][j-1] ;


else


{


d1 = c[i-1][j-1] ;


d2 = c[i-1][j] ;


d3 = c[i][j-1] ;


min = d1<d2 ? d1 : d2 ;


min = min<d3 ? min : d3 ;


c[i][j] = min+1 ;


}


//cout<<c[i][j]<<" ";


}


//cout<<endl;


}


return c[len1][len2] ;


}

[/code]
[/code]

 


[code]
[code] 


bool check( const string& s1 , const string& s2 )


{


int len1 = s1.size() ;


int len2 = s2.size() ;


int i,j ;


if( len1 == len2 )  //replace


  {


i = 0 ;


while( i<len1 && s1[i]==s2[i] )


++i ;


//now , s1[i]!=s2[i] , the edit distance is 1 only when just i-th char is replaced


//other positions are the same , so skip this position


//hello hollo


while( ++i<len1 )


if( s1[i]!=s2[i] )


return false ;


}


else if( len1 == len2+1 )


  {


i = 0 ;


while( i<len2 && s1[i]==s2[i] )


++i ;


//now , s1[i]!=s2[i] , the edit distance is 1 only when s1[i] is been insertd


//other positions are the same , so skip s1[i]


//more mre


while( ++i<len1 )


if( s1[i]!=s2[i-1] )


return false ;


}


else if( len1+1 == len2 )


  {


i = 0 ;


while( i<len1 && s1[i]==s2[i] )


++i ;


//now , s1[i]!=s2[i] , the edit distance is 1 only when s2[i] is been insertd


//other positions are the same , so skip s2[i]


//mre more


while( ++i<len2 )


if( s1[i-1]!=s2[i] )


return false ;


}


else


return false ;


 


return true ;


}


 


//use check


void run1035()


{


vector<string> dictVec ;


dictVec.reserve(10000) ;


vector<string>::iterator dictVecIter ;


 


stdext::hash_set<string> dictSet;


string str,dictStr;


//ifstream in("in.txt");


 


while( cin>>str && str!="#" )


  {


dictVec.push_back(str);


dictSet.insert(str);


}


cin.clear() ;


while( cin>>str && str!="#" )


  {


if( dictSet.find(str)!=dictSet.end() )


 {


cout<<str<<" is correct"<<endl;


continue ;


}


 


cout<<str<<": " ;


for( dictVecIter=dictVec.begin() ; dictVecIter!=dictVec.end() ; ++dictVecIter )


 {


dictStr = *dictVecIter ;


if( check(dictStr,str)==true )


cout<<dictStr<<" " ;


}


cout<<endl ;


}


 


dictVec.clear();


dictSet.clear();


}

[/code]
[/code]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: