Censor
frog is now a editor to censor so-called sensitive words (敏感詞).
She has a long text p. Her job is relatively simple -- just to find the first occurence of sensitive word w and remove it.
frog repeats over and over again. Help her do the tedious work.
Input
The input consists of multiple tests. For each test:
The first line contains 1 string w. The second line contains 1 string p.
(1≤length of w,p≤5⋅106, w,p consists of only lowercase letter)
Output
For each test, write 1 string which denotes the censored text.
Sample Input
abc
aaabcbc
b
bbb
abc
abSample Output
a
ab
上面是題目,大致要求:
W串是可疑串,P是輸入串,在P中將出現的W串刪除,然後輸出P串;
下面是我寫的代碼:
#include<iostream>
#include
using namespace std;
int main()
{
string s1,s2; //s1用於做可疑串,s2用於做輸入串
while(cin>>s1)
{
cin>>s2;
while(-1!=s2.find(s1)) //s2字符串中存在可疑串
{
s2.erase(s2.find(s1),s1.size());
}
cout<<s2<<endl;
}
return 0;
}
求大神編寫一個C++程序,運行時間很低的程序,謝謝各位大牛了;
建議你問這類時空要求高的ACM問題時把題目要求的時限和空間也說出來,這樣也方便別人根據要求選擇合適的算法。
這道題屬於字符串匹配問題,你可以看看**KMP算法**,如果時間還是達不到的話就用**Boyer-Moore算法**吧。代碼就自己搞定喽,做ACM還是盡量不要看別人的代碼比較好