字符串-10115 Automatic Editing

题目大意:根据规则,修改字符串

解题过程:第一次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;
 } 



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值