基础编程题目集 7-29 删除字符串中的子串

7-29 删除字符串中的子串

题目链接-7-29 删除字符串中的子串
在这里插入图片描述
解题思路
STL

  • s.find()判断s1字符串中是否含有s2字符串
  • 然后用s.erase()删除s1中出现的s2字符串
  • 具体操作见代码

s.find()s.erase()用法

  • s.find(a)函数返回字符串s中与字符串a相同的子串的第一个字符的索引(即下标),没找到就返回string::npos
  • s.erase(pos,cnt)删除pos开始的cnt个字符,返回修改后的字符串

附上代码

#include<bits/stdc++.h>
#define int long long
#define lowbit(x) (x &(-x))
#define endl '\n'
using namespace std;
const int INF=0x3f3f3f3f;
const int dir[4][2]={-1,0,1,0,0,-1,0,1};
const double PI=acos(-1.0);
const double eps=1e-10;
const int M=1e9+7;
const int N=1e5+5;
typedef long long ll;
typedef pair<int,int> PII;
string s1,s2;
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	
	getline(cin,s1);
	getline(cin,s2);
	while(s1.find(s2)<s1.length()){
		s1.erase(s1.find(s2),s2.length());
	}	
	cout<<s1;
	return 0;
}

发布了175 篇原创文章 · 获赞 15 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/Fiveneves/article/details/105054447
今日推荐