(大模拟紫题) Luogu P1953 易语言

本文作者MiserWeyte

原题链接:P1953 易语言

(我最近怎么总在做大模拟大搜索题)

分别处理两种情况。

如果只有一个1或0

直接设一个cnt为这个值,每次输入一个新名字之后把数字替换成cnt,最后cnt++即可。

注意数字可能不止一位,输出一次cnt之后可以整一个bool变量阻止以后的输出。

如果是要改名

把名称和后缀分别存起来,扫一遍名称找出初始数字,赋给cnt。

同样,初始数字可能不止一位,要每次乘10加n。

然后不用管输入的是啥,每有一个输入输出一遍初始名称,数字部分替换成cnt即可。然后输出第二个名称时检测到小数点(误)直接输出后缀名之后break。每次循环后cnt++。

然后就WA掉20分

如果数字在后缀名末尾,这样会直接把第二个名称中的数字break掉。这时候之前整的那个bool变量就可以用了。如果数字输出没被阻止(即还没输出过数字),直接输出一次cnt就行。

源码:

//MiserWeyte is now "mzWyt"
#include <bits/stdc++.h>
using namespace std;
string opt1, opt2;
string name1, name2;
int main(){
	cin >> opt1;
	if(opt1[0] == '0' || opt1[0] == '1'){ // 只更换序号 
		int cnt = opt1[0] - '0';
		while(cin >> name1 >> name2){
			bool notend;
			notend = true;
			for(int i=0; i<name1.length(); i++){ // 输入文件文件名 
				if(name1[i]>='0' && name1[i] <= '9'){ // 把数字部分替换为cnt 
					if(notend){ // 防止数字部分有多位,只在第一位时输出一次 
						cout << cnt;
						notend = false;
					}
				}
				else cout << name1[i];
			}
			cout << ' ';
			notend = true;
			for(int i=0; i<name2.length(); i++){ //输出文件文件名同上 
				if(name2[i]>='0' && name2[i] <= '9'){
					if(notend){
						cout << cnt;
						notend = false;
					}
				}
				else cout << name2[i];
			}
			cout << '
';
			cnt ++;
		}
	}
	else{ // 更换名称 
		cin >> opt2; 
		int cnt = 0;
		for(int i=0; i<opt1.length(); i++){ // 防止初始数字有多位 
			if(opt1[i] >= '0' && opt1[i] <= '9'){
				cnt *= 10;
				cnt += opt1[i] - '0';
			}
		}
		while(cin >> name1 >> name2){
			bool notend;
			notend = true;
			for(int i=0; i<opt1.length(); i++){ // 同只改数字的代码,只不过每次以opt1为模板 
				if(opt1[i] >= '0' && opt1[i] <= '9'){
					if(notend){
						cout << cnt;
						notend = false;				
					}
				}
				else cout << opt1[i];
			}
			cout << ' ';
			notend = true;
			for(int i=0; i<opt1.length(); i++){ //输出文件文件名 
				if(opt1[i] >= '0' && opt1[i] <= '9'){
					if(notend){
						cout << cnt;
						notend = false;				
					}
				}
				else{
					cout << opt1[i];
					if(opt1[i] == '.'){ // 点后面的直接替换成指定后缀 
						cout << opt2;
						break;
					}
				}
			}
			if(notend) cout << cnt; // 若还没输出过数字,则数字在后缀名后方,输出一次 
			cout << '
';
			cnt ++;
		}
	}
	return 0;
} 
原文地址:https://www.cnblogs.com/miserweyte/p/11808381.html