by i see » Wed Jun 13, 2012 11:47 am
Hi,the running time of my program exceed time limit,please tell me how to reduce the running time,thanks in advance !
code]
#include <iostream>
#include <string>
#include <fstream>
#define LOCAL
//#define fin cin
//#define fout cout
using namespace std;
int main()
{
#ifdef LOCAL
ifstream fin("in.cpp");
ofstream fout("out.cpp");
#endif
int rule_num,m;
string first[11];
string second[11];
string blank;
string edit;
bool log;
int num=1;
int j;
while(fin>>rule_num)
{
if(rule_num==0) break;
log=true;
getline(fin,blank);
for(int i=0;i<rule_num;)
if(log) {getline(fin,first[i]);log=false;}
else {getline(fin,second[i]);log=true;++i;}
getline(fin,edit);
for(int i=0;i<rule_num;)
{
if(edit.size()>=first[i].size()) {
for(j=0;j<=edit.size()-first[i].size();++j)
{
log=false;
for(m=0;m<first[i].size();++m)
if(first[i][m]!=edit[m+j]) {log=true;break;}
if(!log) {
edit.replace(j,first[i].size(),second[i]);
i=0;
break;
}
}
if(log) ++i;
}
else ++i;
}
fout<<edit<<endl;
}
return 0;
}
[/code]