洛谷 P5734 【深基6.例6】文字处理软件 题解 两种方法 (字符串 C/C++)

  • 方法一:大部分手动实现
//P5734 【深基6.例6】文字处理软件
//#define LOCAL
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <cctype>
#define inf 0x3f3f3f3f
#define eps 1e-6
using namespace std;
#define clr(x) memset(x,0,sizeof((x)))
const int maxn = 1e4+1;//2e6+1
#define MAX(a,b,c) ((a)>(b)?((a)>(c)?(a):(c)):((b)>(c)?(b):(c)))
#define _max(a,b) ((a) > (b) ? (a) : (b))
#define _min(a,b) ((a) < (b) ? (a) : (b))
#define _for(a,b,c) for(int a = b;a<c;a++)
char s[maxn],tmp[maxn];
//拼接
void f1() {
    
    
	char str[100];
	clr(str);
	scanf("%s",str);
	strcat(s,str);
}

//截取
void f2() {
    
    
	int a,b,j = 0;
	cin>>a>>b;
	for(int i = a;i<a+b;i++) {
    
    
		tmp[j++] = s[i];
	}
	clr(s);
	strcpy(s,tmp);
}

//插入
void f3() {
    
    
	int a;
	char ss[105];
	scanf("%d%s",&a,ss);
	int len = strlen(ss);
	int lens = strlen(s);
	for(int i = lens-1;i>=a;i--) {
    
    
		s[i+len] = s[i];
	}
	int cnt = 0;
	for(int j = a;j<a+len;j++) {
    
    
		s[j] = ss[cnt++];
	}
}

//查找
int f4() {
    
    
	char st[105];
	scanf("%s",st);
	int i,j = 0,flag = 0;
	for(i = 0;i<strlen(s);i++) {
    
    
		if(s[i]!=st[j]) {
    
    
			if(j!=0)i--;
			j = 0;
			continue;
		}
		j++;
		if(j==strlen(st)) {
    
    
			flag = 1;break;
		}
	}
	if(flag)return (i-strlen(st)+1);
	return -1;
}
void _print() {
    
    
	_for(i,0,strlen(s)){
    
    
		cout<<s[i];
	}
	cout<<endl;
}
int main() {
    
    
#ifdef LOCAL 
	freopen("data.in","r",stdin);
	freopen("data.out","w",stdout);
#endif
	int q;
	cin>>q;
	scanf("%s",s);
	while(q--) {
    
    
		int cmd;
		scanf("%d",&cmd);
		switch(cmd) {
    
    
			case 1 :f1();break;
			case 2 :f2();break;
			case 3 :f3();break;
			case 4 :cout<<f4()<<endl;break;
		}
		if(cmd!=4)_print();
	}
	return 0;
}
  • 方法二:STL 库函数
//#define LOCAL
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <cctype>
#define inf 0x3f3f3f3f
#define eps 1e-6
using namespace std;
#define clr(x) memset(x,0,sizeof((x)))
const int maxn = 1e4+1;//2e6+1
#define MAX(a,b,c) ((a)>(b)?((a)>(c)?(a):(c)):((b)>(c)?(b):(c)))
#define _max(a,b) ((a) > (b) ? (a) : (b))
#define _min(a,b) ((a) < (b) ? (a) : (b))
#define _for(a,b,c) for(int a = b;a<c;a++)
char s[maxn],tmp[maxn];
int n,opc,op1,op2;
string str,s1;
int main()
{
    
    
#ifdef LOCAL 
	freopen("data.in","r",stdin);
	freopen("data.out","w",stdout);
#endif
	cin>>n;
	cin>>str;
	for(int i=0;i<n;i++)
	{
    
    
		cin>>opc;
		if(opc==1)//拼接 (string类型直接+就好了)
		{
    
    
			cin>>s1;
			str+=s1;
			cout<<str<<endl;
		}
		else if(opc==2)//截取 substr
		{
    
    
			cin>>op1>>op2;
			s1=str.substr(op1,op2);
			str=s1;
			cout<<str<<endl;
		}
		else if(opc==3)//插入 insert
		{
    
    
			cin>>op1>>s1;
			str.insert(op1,s1);
			cout<<str<<endl;
		}
		else if(opc==4)//查找 find
		{
    
    
			cin>>s1;
			size_t loc = str.find(s1,0);//size_t的真实类型与操作系统有关。
			if(loc!=string::npos)
				cout<<loc<<endl;
			else cout<<-1<<endl;
		}
	}
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Jason__Jie/article/details/112543763
今日推荐