敏感词
题目链接: http://exercise.acmcoder.com/online/online_judge_ques?ques_id=3354&konwledgeId=40
解题思路: 题目没有给出字符串的范围,不过还是比较小的,直接暴力判断了。
1 #include <bits/stdc++.h> 2 using namespace std; 3 4 typedef long long LL; 5 const int MAXN=100005; 6 const LL MON7 = 1e9+7; 7 string s1[MAXN]; 8 char s[MAXN]; 9 int n; 10 bool mask[MAXN]; 11 12 char lower(char ch) 13 { 14 if (ch>='A' && ch<='Z') return ch-'A'+'a'; 15 return ch; 16 } 17 18 int main() 19 { 20 // cout<<lower('G')<<endl; 21 #ifndef ONLINE_JUDGE 22 freopen("test.txt","r",stdin); 23 #endif // ONLINE_JUDGE 24 scanf("%d",&n); 25 for (int i=1;i<=n;++i) 26 { 27 scanf("%s",s); 28 for (int j=0;s[j];++j) 29 { 30 if (s[j]>='A' && s[j]<='Z') s[j]=s[j]-'A'+'a'; 31 } 32 s1[i]=s; 33 //cout<<s1[i]<<endl; 34 } 35 getchar(); 36 gets(s);getchar(); 37 //puts(s); 38 int Len=strlen(s); 39 memset(mask, 0, sizeof(mask)); 40 for (int i=0;i<Len;++i) 41 { 42 if (!isalpha(s[i])) continue; 43 //printf("s=%s\n",s+i); 44 for (int j=1;j<=n;++j) 45 { 46 if (i+s1[j].length()>Len) continue; 47 int flags=1; 48 for (int k=0;k<s1[j].length();++k) 49 { 50 //if (s1[j]=="gre") printf("%c==%c\n",s[i+k],lower(s1[j][k])); 51 if (lower(s[i+k])!=s1[j][k]) {flags=0;break;} 52 } 53 //printf("s1[%d]=%s flags=%d\n",j,s1[j].c_str(), flags); 54 if (flags) 55 { 56 for (int k=0;k<s1[j].length();++k) mask[i+k]=1; 57 } 58 } 59 } 60 for (int i=0;i<Len;++i) 61 { 62 if (mask[i]) printf("*"); 63 else printf("%c",s[i]); 64 } 65 printf("\n"); 66 return 0; 67 }