0
点赞
收藏
分享

微信扫一扫

C. Good String (字符串+操作模拟)Codeforces Round #560 (Div. 3)

原题链接:​​https://codeforces.com/contest/1165/problem/C​​​C. Good String  (字符串+操作模拟)Codeforces Round #560 (Div. 3)_字符串
样例:

Input
4
good
Output
0
good
Input
4
aabc
Output
2
ab
Input
3
aaa
Output
3

题意: 给定一个字符串,你需要进行删除操作使得它满足新式字符串的要求,问最小的修改次数。

解题思路: 这道题我们对字符串模拟即可,由于已经维护好了的不受影响,故我们可以用一个字符动态数组存储新式字符串。通过遍历字符串来模拟这个删除操作即可,这样我们自然可以求出最小的修改次数,具体看代码实现。

AC代码:

/*

*
*/
#include<bits/stdc++.h> //POJ不支持

#define rep(i,a,n) for (int i=a;i<=n;i++)//i为循环变量,a为初始值,n为界限值,递增
#define per(i,a,n) for (int i=a;i>=n;i--)//i为循环变量, a为初始值,n为界限值,递减。
#define pb push_back
#define IOS ios::sync_with_stdio(false);cin.tie(0); cout.tie(0)
#define fi first
#define se second
#define mp make_pair

using namespace std;

const int inf = 0x3f3f3f3f;//无穷大
const int maxn = 1e5;//最大值。
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
//*******************************分割线,以上为自定义代码模板***************************************//

int main(){
//freopen("in.txt", "r", stdin);//提交的时候要注释掉
IOS;
int n;
string str;
while(cin>>n){
cin>>str;
vector<char> s;
char ch;
int sum=0,temp;
rep(i,0,n-1){
temp=s.size();
if(temp!=0&&temp%2){
ch=s.back();
if(ch==str[i]){
sum++;
continue;
}
else{
s.push_back(str[i]);
}
}
else{
s.push_back(str[i]);
}
}
temp=s.size();
if(temp%2){s.pop_back();sum++;}
cout<<sum<<endl;
temp=s.size();
rep(i,0,temp-1)
cout<<s[i];
if(temp!=0)cout<<endl;

}
return 0;
}


举报

相关推荐

0 条评论