1089. Verification with a Vocabulary
Time limit: 2.0 second
Memory limit: 64 MB
Memory limit: 64 MB
Your teacher of English has recently told you that she dreams about the automated system of correction and count of mistakes that her pupils do very frequently.The 8
th of March is soon and you've decided that if you write such program and you'll present your teacher with it, then she will be glad and may be will be more favorable to you at the exam. The task is to replace words that differ from the ones in the vocabulary not more than by a letter and to count an amount of the made mistakes. Your teacher must have forgotten that her pupils sometimes forget to write letters and even sometimes they write extra letters - excellent! You are not to think about it!
Input
In the first line and then up to the line containing the only symbol `#' go vocabulary words, each word in its line. In the lines that go after the one containing `#' goers a text that you are to check-up. There are not more than 100 words in the vocabulary. A length of a word in the vocabulary doesn't exceed 8 symbols. A length of the text being checked-up doesn't exceed 1000 words. All the words are written in the lower case. A word might contain Latin letters from `a' to `z'. The vocabulary is compiled so that for each word not more than one variant of correction is possible. There's exactly one end of line character at the end of the text.
Output
You are to output the corrected text in the same format as the initial one (the same punctuation marks, line breaks and words that are absent in the vocabulary) and in the line next to the text there is to be a number of corrections.
Sample
input |
---|
country occupies surface covers russia largest europe part about world # the rushia is the larjest cauntry in the vorld. it ockupies ebout one-seventh of the earth's surfase. it kovers the eastern park of yurope and the northern park of asia. |
output |
the russia is the largest country in the world. it occupies about one-seventh of the earth's surface. it covers the eastern part of europe and the northern part of asia. 11 |
做的过程中的难点就是想不到如何从字符串中提取单词,如何读入文件?当然这个题给了我很大的启发,可以单个字符处理。
其实这种做法,上一道题也有类似的做法,自己没想到。。
总之还是有收获的,不要纠结于暂时的得与失。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
char doc[3005][15];
bool check(char a[],char b[])
{
int len=strlen(a),len1=strlen(b),count=0;
if(len!=len1)return false;
for(int i=0;i<len;i++)
{
if(a[i]!=b[i])count++;
}
if(count==1)return true;
else return false;
}
int main()
{
// freopen("q.in","r",stdin);
int i,j;
int total=0;
char s[15]; //因为文件里的单词可能超过10,而文件里的单词也是暂存在这里的,所以数组要开的大些。开小了WA。。。。
char ch;
int res=0,cnt;
while(~scanf("%s",s) && s[0]!='#')
strcpy(doc[total++],s);
getchar();
cnt=0;
memset(s,0,sizeof(s)); //存单词
while(~scanf("%c",&ch))
{
if(!isalpha(ch))
{
bool flag=false;
for(i=0;i<total;i++)
{
if(check(s,doc[i]))
{
res++;
cout<<doc[i];
flag=true;
break;
}
}
if(!flag)
{
cout<<s;
}
cout<<ch;
memset(s,0,sizeof(s));
cnt=0;
}
else
{
s[cnt++]=ch;
}
}
cout<<endl;
cout<<res<<endl;
}