http://ac.jobdu.com/problem.php?cid=1040&pid=104
1、str1.find(str2,pos); 若循環查找,pos+str2.size(),查找完,跳過當前這個位置再查找
2、str1.replace(pos1,num,str2); 替換位置,替換個數,目標字符串
3、出現TE,因爲循環查找處問題
#include <iostream>
#include <string>
#include <stdio.h>
using namespace std;
int main()
{
string s,a,b;
while (getline(cin,s))
{
getline(cin,a);
getline(cin,b);
int t=s.find(a,0);
while (t!=string::npos)
{
int flag=0;
if (t==0) //單詞頭
{
if (t+a.size()==s.size()) //僅有一個單詞
{
flag=1;
}
else if (s[t+a.size()]==' ') //多個單詞
{
flag=1;
}
}
else if (t+a.size()==s.size()) //單詞尾
{
if (s[t-1]==' ')
{
flag=1;
}
}
else if (s[t-1]==' '&&s[t+a.size()]==' ')
{
flag=1;
}
if (flag==1)
{
s.replace(t,a.size(),b); //替換位置,替換個數,目標字符串
}
t=s.find(a,t+a.size()); //循環查找,上次查找到位置t處後面a.size()個之後繼續查找
}
cout<<s<<endl;
}
return 0;
}