10115 please tell me how to reduce the running times !

All about problems in Volume CI. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

10115 please tell me how to reduce the running times !

Postby 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]
i see
New poster
 
Posts: 9
Joined: Sat May 19, 2012 7:35 am

Re: 10115 please tell me how to reduce the running times !

Postby brianfry713 » Wed Jun 13, 2012 11:37 pm

Don't read and write to a file
brianfry713
Guru
 
Posts: 1761
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 10115 please tell me how to reduce the running times !

Postby i see » Fri Jun 15, 2012 6:36 pm

thank you,i have passed !
i see
New poster
 
Posts: 9
Joined: Sat May 19, 2012 7:35 am

Re: 10115 please tell me how to reduce the running times !

Postby Mukit Chowdhury » Fri Aug 17, 2012 9:48 pm

Cut after Accepted !!! :)
Last edited by Mukit Chowdhury on Sun Sep 16, 2012 11:12 am, edited 2 times in total.
Mukit Chowdhury
Learning poster
 
Posts: 59
Joined: Fri Aug 17, 2012 9:23 pm
Location: CUET

Re: 10115 please tell me how to reduce the running times !

Postby brianfry713 » Sat Aug 18, 2012 1:39 am

Don't use a single getchar() and count on it being a newline, that won't work with trailing spaces.
brianfry713
Guru
 
Posts: 1761
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 10115 please tell me how to reduce the running times !

Postby Mukit Chowdhury » Sun Sep 16, 2012 11:07 am

Got Accepted !!! :)
Mukit Chowdhury
Learning poster
 
Posts: 59
Joined: Fri Aug 17, 2012 9:23 pm
Location: CUET


Return to Volume CI

Who is online

Users browsing this forum: No registered users and 1 guest