http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=96&page=show_problem&problem=1056
题意:
进行字符串替换。输入几个规则。每个规则都是a string to find & a string to replace。最后是一行待处理字符串。
每个规则找的时候,每次都是从左到右找~注意
解题:
用string & string.find & string.replace解决。。。(PS:想起几年前去机试,那个时候不会string和str,做不出来,好丢人。。。)
最近喜欢用vector & string来存东西,哈哈,不用理会大小~
#include <iostream>
#include <string>
#include <vector>
#include <stdio.h>
using namespace std;
int main()
{
int nRule;
while ( cin >>nRule )
{
if ( nRule == 0 )
{
break;
} // end if
// ignore the \n that just input
cin.ignore();
vector <string> vCode;
vector <string> vCodeRe;
string strIn;
for ( int i=0; i<nRule; i++ )
{
// input string to find
getline(cin, strIn);
vCode.push_back(strIn);
// input string to replace
getline(cin, strIn);
vCodeRe.push_back(strIn);
} // end for
// get target string
string tarStr;
getline(cin, tarStr);
// cout <<"Before: " <<tarStr <<"\n";
// for each rule
for ( int i=0; i<nRule; i++ )
{
// one rule round
int nPos = 0;
while ( 1 )
{
nPos = tarStr.find(vCode[i], 0);
if ( nPos == string::npos )
{
break;
} // end if
else
{
tarStr.replace(nPos, vCode[i].length(), vCodeRe[i]);
} // end else
} // end while
} // end for
// cout <<"After: " <<tarStr <<"\n";
// cout <<"\n";
cout <<tarStr <<"\n";
} // end while
return 0;
}