HDU 1483 Automatic Correction of Misspellings (字符串大模拟)
2016-10-23 11:15
369 查看
Description
Some text editors offer a feature to correct words which seem to be written incorrectly. In this problem you are asked to implement a simple Automatic Correction of Misspellings (ACM).
ACM takes care of the following misspellings of words:
1.One letter is missing (e.g., letter is written leter) or too much (e.g., letter is written lettter).
2.One letter is wrong (e.g., letter is written ketter)
3.The order of two adjacent letters is wrong (e.g., letter is written lettre)
ACM is based on a dictionary of known words. When a text contains a word which is not in the dictionary, ACM will try to replace it by a similar word of the dictionary. Two words are similar if we can transform one word into the other by doing exactly one of
the misspellings listed above. An unknown word is left unchanged if there is no similar word in the dictionary.
Input
The first line of the input file will give the number n of words in the dictionary (n ≤ 10000). The next n lines contain the dictionary words. The following line contains an integer q ≤ 1000, the number of query words. The next q lines contain the query words.
You may assume that each word in the input consists of 1 to 25 lower case letters ('a' to 'z').
Output
For each query word, print one line with the query word followed by one of the following possibilities:
1.is correct, if the word occurs in the dictionary.
2.is a misspelling of <x>, where <x> is a word of the dictionary similar to the query word, and the query word is not in the dictionary. In the case that there are several possibilities, select the word from the dictionary which appeared earlier in the input.
3.is unknown, if cases 1 and 2 do not apply.
Sample Input
Sample Output
这个题是说先给你一个n,然后输入n个字符串(当作字典)。然后再输入q,再输入q个字符串,,最后分别看这q个字符串和这个字典的关系。
1.字典中字符串是比该字串长度少1的子串,或者反过来也可以。
2.相邻两个字符相反。
3.只允许有一个字符出现错误。
4.该字符串在字典中。
5.如果不符合以上四个条件的,则说明未出现过。
6.当有多个符合条件的字符串时,优先出现的优先选择。
就是暴力+模拟。
#include <iostream>
#include <cstdio>
#include <string>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <map>
#include <queue>
#include <stack>
#include <functional>
#define INF 0x3f3f3f3f
#define mem(a,b) memset(a,b,sizeof(a));
#define For(a,b) for(int i = a;i<b;i++)
#define LL long long
#define MAX_N 100010
using namespace std;
char z[10005][100],x[1005][100];
int n,k;
inline bool compare(int cur)
{
for(int i = 0; i<n; i++)
{
if(!strcmp(x[cur],z[i]))
{
return true;
}
}
return false;
}
inline bool compare1(int cur,int *pos)//zichuan
{
for(int i = 0; i<n; i++)
{
int ans = 0;
*pos = i;
int sum = 0,sum1 = 0;
int len = strlen(x[cur]);
int len1 = strlen(z[i]);
if(abs(len - len1) == 1)
{
if(len < len1)
{
int xx = 0;
sum = 0;
for(int j = 0; j<len1; j++)
{
if(x[cur][xx] == z[i][j])
xx ++;
else
sum ++;
}
}
else
{
int zz = 0;
sum1 = 0;
for(int j = 0; j<len; j++)
{
if(x[cur][j] == z[i][zz])
zz ++;
else
sum1 ++;
}
}
if(sum == 1 || sum1 == 1)
{
//cout<<"zichuan"<<endl;
return true;
}
}
}
return false;
}
inline bool compare2(int cur,int *pos)//wrong
{
for(int i = 0; i<n; i++)
{
*pos = i;
int ans = 0;
if(strlen(x[cur]) == strlen(z[i]))
{
for(int j = 0; j<strlen(z[i]); j++)
{
if(x[cur][j] != z[i][j])
{
ans ++;
}
}
if(ans == 1)
{
//cout<<"wrong"<<endl;
return true;
}
}
}
return false;
}
inline bool compare3(int cur,int *pos)//daoxu
{
int getc = 0;
int a[10010];
int len = strlen(x[cur]);
for(int i = 0; i<len; i++)
getc += (x[cur][i]-'a');
for(int i = 0; i<n; i++)
{
*pos = i;
int len1 = strlen(z[i]),getc1 = 0;
if(len == len1)
{
for(int j = 0; j<len1; j++)
getc1 += (z[i][j]-'a');
if(getc != getc1) continue;
else
{
int ans = 0;
for(int k = 0; k<len; k++)
{
if(x[cur][k] != z[i][k])
{
a[ans++] = k;
}
}
if(ans == 2)
{
if(x[cur][a[0]] == z[i][a[1]])
{
if(x[cur][a[1]] == z[i][a[0]])
{
//cout<<"daoxu"<<endl;
return true;
}
}
}
}
}
}
return false;
}
int main()
{
scanf("%d",&n);
for(int i = 0; i<n; i++)
scanf("%s",&z[i]);
scanf("%d",&k);
for(int i = 0; i<k; i++)
scanf("%s",&x[i]);
for(int i = 0; i<k; i++)
{
int pp = INF,pos = INF;
bool flag = false;
if(compare(i))
{
printf("%s is correct\n",x[i]);
continue;
}
if(compare1(i,&pos))
{
flag = true;
pp = min(pp,pos);
}
if(compare2(i,&pos))
{
flag = true;
pp = min(pp,pos);
}
if(compare3(i,&pos))
{
flag = true;
pp = min(pp,pos);
}
if(flag) printf("%s is a misspelling of %s\n",x[i],z[pp]);
else printf("%s is unknown\n",x[i]);
}
return 0;
}
Some text editors offer a feature to correct words which seem to be written incorrectly. In this problem you are asked to implement a simple Automatic Correction of Misspellings (ACM).
ACM takes care of the following misspellings of words:
1.One letter is missing (e.g., letter is written leter) or too much (e.g., letter is written lettter).
2.One letter is wrong (e.g., letter is written ketter)
3.The order of two adjacent letters is wrong (e.g., letter is written lettre)
ACM is based on a dictionary of known words. When a text contains a word which is not in the dictionary, ACM will try to replace it by a similar word of the dictionary. Two words are similar if we can transform one word into the other by doing exactly one of
the misspellings listed above. An unknown word is left unchanged if there is no similar word in the dictionary.
Input
The first line of the input file will give the number n of words in the dictionary (n ≤ 10000). The next n lines contain the dictionary words. The following line contains an integer q ≤ 1000, the number of query words. The next q lines contain the query words.
You may assume that each word in the input consists of 1 to 25 lower case letters ('a' to 'z').
Output
For each query word, print one line with the query word followed by one of the following possibilities:
1.is correct, if the word occurs in the dictionary.
2.is a misspelling of <x>, where <x> is a word of the dictionary similar to the query word, and the query word is not in the dictionary. In the case that there are several possibilities, select the word from the dictionary which appeared earlier in the input.
3.is unknown, if cases 1 and 2 do not apply.
Sample Input
10 this is a dictionary that we will use for us 6 su as the dictonary us willl
Sample Output
su is a misspelling of us as is a misspelling of is the is unknown dictonary is a misspelling of dictionary us is correct willl is a misspelling of will
这个题是说先给你一个n,然后输入n个字符串(当作字典)。然后再输入q,再输入q个字符串,,最后分别看这q个字符串和这个字典的关系。
1.字典中字符串是比该字串长度少1的子串,或者反过来也可以。
2.相邻两个字符相反。
3.只允许有一个字符出现错误。
4.该字符串在字典中。
5.如果不符合以上四个条件的,则说明未出现过。
6.当有多个符合条件的字符串时,优先出现的优先选择。
就是暴力+模拟。
#include <iostream>
#include <cstdio>
#include <string>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <map>
#include <queue>
#include <stack>
#include <functional>
#define INF 0x3f3f3f3f
#define mem(a,b) memset(a,b,sizeof(a));
#define For(a,b) for(int i = a;i<b;i++)
#define LL long long
#define MAX_N 100010
using namespace std;
char z[10005][100],x[1005][100];
int n,k;
inline bool compare(int cur)
{
for(int i = 0; i<n; i++)
{
if(!strcmp(x[cur],z[i]))
{
return true;
}
}
return false;
}
inline bool compare1(int cur,int *pos)//zichuan
{
for(int i = 0; i<n; i++)
{
int ans = 0;
*pos = i;
int sum = 0,sum1 = 0;
int len = strlen(x[cur]);
int len1 = strlen(z[i]);
if(abs(len - len1) == 1)
{
if(len < len1)
{
int xx = 0;
sum = 0;
for(int j = 0; j<len1; j++)
{
if(x[cur][xx] == z[i][j])
xx ++;
else
sum ++;
}
}
else
{
int zz = 0;
sum1 = 0;
for(int j = 0; j<len; j++)
{
if(x[cur][j] == z[i][zz])
zz ++;
else
sum1 ++;
}
}
if(sum == 1 || sum1 == 1)
{
//cout<<"zichuan"<<endl;
return true;
}
}
}
return false;
}
inline bool compare2(int cur,int *pos)//wrong
{
for(int i = 0; i<n; i++)
{
*pos = i;
int ans = 0;
if(strlen(x[cur]) == strlen(z[i]))
{
for(int j = 0; j<strlen(z[i]); j++)
{
if(x[cur][j] != z[i][j])
{
ans ++;
}
}
if(ans == 1)
{
//cout<<"wrong"<<endl;
return true;
}
}
}
return false;
}
inline bool compare3(int cur,int *pos)//daoxu
{
int getc = 0;
int a[10010];
int len = strlen(x[cur]);
for(int i = 0; i<len; i++)
getc += (x[cur][i]-'a');
for(int i = 0; i<n; i++)
{
*pos = i;
int len1 = strlen(z[i]),getc1 = 0;
if(len == len1)
{
for(int j = 0; j<len1; j++)
getc1 += (z[i][j]-'a');
if(getc != getc1) continue;
else
{
int ans = 0;
for(int k = 0; k<len; k++)
{
if(x[cur][k] != z[i][k])
{
a[ans++] = k;
}
}
if(ans == 2)
{
if(x[cur][a[0]] == z[i][a[1]])
{
if(x[cur][a[1]] == z[i][a[0]])
{
//cout<<"daoxu"<<endl;
return true;
}
}
}
}
}
}
return false;
}
int main()
{
scanf("%d",&n);
for(int i = 0; i<n; i++)
scanf("%s",&z[i]);
scanf("%d",&k);
for(int i = 0; i<k; i++)
scanf("%s",&x[i]);
for(int i = 0; i<k; i++)
{
int pp = INF,pos = INF;
bool flag = false;
if(compare(i))
{
printf("%s is correct\n",x[i]);
continue;
}
if(compare1(i,&pos))
{
flag = true;
pp = min(pp,pos);
}
if(compare2(i,&pos))
{
flag = true;
pp = min(pp,pos);
}
if(compare3(i,&pos))
{
flag = true;
pp = min(pp,pos);
}
if(flag) printf("%s is a misspelling of %s\n",x[i],z[pp]);
else printf("%s is unknown\n",x[i]);
}
return 0;
}
相关文章推荐
- SDUT 2364 || POJ 2934 Automatic Correction of Misspellings(简单字符串处理)
- 【HDOJ】1483 Automatic Correction of Misspellings
- Automatic Correction of Misspellings
- 暴搜。。。HOJ2297 Automatic Correction of Misspellings。
- HDU 1088 Write a simple HTML Browser && 字符串模拟
- hdu 4119 (模拟+字符串)成都现场赛I题
- HDU 4119Isabella's Message2011成都现场赛I题(字符串模拟)
- HDU 4119Isabella's Message2011成都现场赛I题(字符串模拟)
- hdu 2412 (disney) 模拟、字符串处理
- HDU-#5018 Revenge of Fibonacci(模拟)
- HDU 1237 简单计算器 && 字符串模拟四则运算
- 【字符串模拟TWO】我又手贱了-HDU 1177 "AC TODAY"
- HDU-#4432 Sum of divisors(模拟)
- hdu 2054 (典型字符串模拟)
- HDU 3257 Hello World!(字符串找规律——位运算来模拟过程)
- HDU 1736(字符串模拟的水题)
- HDU 1753 大明A+B(字符串模拟,简单题)
- hdu 1062 Text Reverse(字符串处理,简单模拟)
- hdu 3753 字符串 Alignment of Code
- HDU 4740 The Donkey of Gui Zhou (模拟)