字符串-10115 Automatic Editing
2016-07-26 22:04
357 查看
题目大意:根据规则,修改字符串
解题过程:第一次Time limit exceeded,分析原因是忽略了题目中的一个提示,已经使用完毕的规则将不再使用,改正之后AC
正确代码:# include <cstdio>
# include <ctime>
# include <cmath>
# include <iostream>
# include <fstream>
# include <cstring>
# include <string>
//*
#define fin cin
#define fout cout
//*/
using namespace std;
/*
ifstream fin("in.txt");
ofstream fout("out.txt");
//*/
struct rule{
char find[100];
char replace_by[100];
};
rule rules[10];
char temp[500];
bool findString(char* temp,rule rule0){
char a[500];
char find[100];
char replace_by[100];
char stringf[500];
char stringb[500];
strcpy(find,rule0.find);
strcpy(replace_by,rule0.replace_by);
int i=strchr(temp,find[0])-temp;
for(;i<strlen(temp);i++){
if(temp[i]==find[0]){
strncpy(a,temp+i,strlen(find));
a[strlen(find)]='\0';
if(strcmp(a,find)==0){
// fout<<"替换位置:"<<i<<endl;
strncpy(stringf,temp,i);
stringf[i]='\0';
strncpy(stringb,temp+i+strlen(find),strlen(temp)-i-strlen(find));
stringb[strlen(temp)-i-strlen(find)]='\0';
// fout<<temp<<endl;
// fout<<"前段:"<<stringf<<endl;
// fout<<"中段:"<<replace_by<<endl;
// fout<<"后段:"<<stringb<<endl;
temp[0]='\0';
strcat(temp,stringf);
strcat(temp,replace_by);
strcat(temp,stringb);
return true;
}
}
}
return false;
}
int main()
{
int n;
fin>>n;
while(n){
fin.getline(temp,100);
for(int i=0;i<n;i++){
fin.getline(rules[i].find,100);
fin.getline(rules[i].replace_by,100);
}
fin.getline(temp,500);
int orderofrules=0;
while(orderofrules<n){
int i;
for(i=orderofrules;i<n;i++){
if(findString(temp,rules[i])){
break;
}
}
orderofrules=i;
}
fout<<temp<<endl;
fin>>n;
}
return 0;
}
解题过程:第一次Time limit exceeded,分析原因是忽略了题目中的一个提示,已经使用完毕的规则将不再使用,改正之后AC
正确代码:# include <cstdio>
# include <ctime>
# include <cmath>
# include <iostream>
# include <fstream>
# include <cstring>
# include <string>
//*
#define fin cin
#define fout cout
//*/
using namespace std;
/*
ifstream fin("in.txt");
ofstream fout("out.txt");
//*/
struct rule{
char find[100];
char replace_by[100];
};
rule rules[10];
char temp[500];
bool findString(char* temp,rule rule0){
char a[500];
char find[100];
char replace_by[100];
char stringf[500];
char stringb[500];
strcpy(find,rule0.find);
strcpy(replace_by,rule0.replace_by);
int i=strchr(temp,find[0])-temp;
for(;i<strlen(temp);i++){
if(temp[i]==find[0]){
strncpy(a,temp+i,strlen(find));
a[strlen(find)]='\0';
if(strcmp(a,find)==0){
// fout<<"替换位置:"<<i<<endl;
strncpy(stringf,temp,i);
stringf[i]='\0';
strncpy(stringb,temp+i+strlen(find),strlen(temp)-i-strlen(find));
stringb[strlen(temp)-i-strlen(find)]='\0';
// fout<<temp<<endl;
// fout<<"前段:"<<stringf<<endl;
// fout<<"中段:"<<replace_by<<endl;
// fout<<"后段:"<<stringb<<endl;
temp[0]='\0';
strcat(temp,stringf);
strcat(temp,replace_by);
strcat(temp,stringb);
return true;
}
}
}
return false;
}
int main()
{
int n;
fin>>n;
while(n){
fin.getline(temp,100);
for(int i=0;i<n;i++){
fin.getline(rules[i].find,100);
fin.getline(rules[i].replace_by,100);
}
fin.getline(temp,500);
int orderofrules=0;
while(orderofrules<n){
int i;
for(i=orderofrules;i<n;i++){
if(findString(temp,rules[i])){
break;
}
}
orderofrules=i;
}
fout<<temp<<endl;
fin>>n;
}
return 0;
}
相关文章推荐
- FTP主动与被动传输引发的协议那些事
- 第20条:类层次优先于标签类
- 深度学习斯坦福cs231n 课程笔记
- Java的基础知识4——容器
- UE4 启动器一系列问题处理链接
- ID卡的原理和ID卡复制器
- 顺序表应用5:有序顺序表归并
- 0726linux基础内容小记
- 企业信息化快速开发平台 JeeSite
- MySQL学习系列一---命令行连接mysql和执行sql文件
- C#使用Process类杀死进程,执行命令等
- redis学习笔记之list类型
- 使用Spring4+Maven构建webapp项目
- 哈希函数相关
- 签到
- MongoDB 删除集合
- MongoDB 删除集合
- MongoDB 删除集合
- 设计模式——抽象工厂
- 51nod 多重背包问题(动态规划)