题目描述
互联网中存在许多的不和谐词汇,在我们浏览互联网的时候,搜索引擎经常会出现提示信息“根据相关法律法规和政策,部分搜索结果未予显示”。小王同学也想制作一个自己的屏蔽词库,这样他就可以将自己不喜欢的词汇从文件中换成别的字符了。屏蔽词库是一个ASCII码文件,这个文件中只含有单词,每个单词占一行,每个单词中只可能有大小写字母与空格。题目中和谐词库的文件名为dict.dic。(屏蔽词库中每个词汇长度小于10,屏蔽词汇个数不超过10.)
你的任务是将输入中的所有和谐词库中的词语全部替换成“!@#
%^&*”(按住键盘shift和数字1至8),然后输出。这里要注意,如果一个词语中包含屏蔽词汇,那么只将屏蔽词汇替换,例如“hehasAAA”被处理后将得到“he!@#
%^&*AAA”,注意屏蔽词汇区分大小写,即aaa与AAA是两个不同的单词,为了使问题简化,屏蔽词汇中不会出现互相包含的情况,如“xabcx”与“abc”不会同时出现在同一个屏蔽词库中。由于小王同学很不擅长文件操作,所以他希望你能帮他制作这个屏蔽词汇程序。
输入
若干长度小于110的字符串。
输出
添加屏蔽词后的结果。
样例输入
The night falls gently. And you are not here. I missing you more and more and I start getting worried as
I stare at the door just waiting for you to surprise me with your arrival at any moment.
Sweet delusion… you are so far away right now that all I can ask for is that time moves faster…
样例输出
如果屏蔽词库如下:(文件中每行都有换行符)
is
good
are
the
ha ha
some
get
has
more
bad
则输出:
The night falls gently. And you !@#$%^&* not here. I m!@#$%^&*sing you !@#$%^&* and !@#$%^&* and I start !@#$%^&*ting worried as
I st!@#$%^&* at !@#$%^&* door just waiting for you to surpr!@#$%^&*e me with your arrival at any moment.
Sweet delusion... you !@#$%^&* so far away right now that all I can ask for !@#$%^&* that time moves faster...
源代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void restart(int *,int *);
int main(){
FILE * fp = fopen("dict.dic","r");
char dictionary[15][15] = {0};
char line[400] = {0};
int i = 0,j = 0;
char ch;
int start[111],end[111];
restart(start,end);
while(!feof(fp)){
ch = (char)fgetc(fp);
while(ch != '\n' && ch != EOF){
dictionary[i][j] = ch;
ch = (char)fgetc(fp);
j++;
}
j = 0;
i++;
}
gets(line);
while(line[0] != 0){
int k,l = 0;
int flag1 = 1,flag2;
for(i = 0;i < 400 && flag1;){
int temp1 = i;
if(line[i] == 0) flag1 = 0;
else{
flag2 = 1;
for(j = 0;j < 11 && flag2;j++){
int on = i;
while(line[i] == dictionary[j][k] && line[i]){
k++;
i++;
}
if(dictionary[j][k] == 0 && dictionary[j][0] != 0){
start[l] = on;
end[l] = i;
l++;
k = 0;
flag2 = 0;
}
else{
i = on;
k = 0;
}
}
}
if(flag2){
i = temp1;
i++;
}
}
j = 0;
for(i = 0;i <400 && line[i];i++){
while((i < start[j]) && i < 400 && line[i]){
putchar(line[i]);
i++;
}
if(start[j] != 200) printf("!@#$%^&*");
i = end[j]-1;
j++;
}
printf("\n");
restart(start,end);
memset(line,0,400);
gets(line);
}
return 0;
}
void restart(int * start,int * end){
int i;
for(i = 0;i < 111;i++){
start[i] = 200;
end[i] = 200;
}
}
教训
原本为了算法复杂度用了一种更复杂的用链表的算法,死活过不去…换了数组之后就好很多了,直接暴力解决,今后要注意看题目的要求,能暴力求解的就直接暴力