Codeforces - #560 - C.Good String

Let’s call (yet again) a string good if its length is even, and every character in odd position of this string is different from the next character (the first character is different from the second, the third is different from the fourth, and so on). For example, the strings good, string and xyyx are good strings, and the strings bad, aa and aabc are not good. Note that the empty string is considered good.

You are given a string s, you have to delete minimum number of characters from this string so that it becomes good.

Input
The first line contains one integer n (1≤n≤2⋅105) — the number of characters in s.

The second line contains the string s, consisting of exactly n lowercase Latin letters.

Output
In the first line, print one integer k (0≤k≤n) — the minimum number of characters you have to delete from s to make it good.

In the second line, print the resulting string s. If it is empty, you may leave the second line blank, or not print it at all.

Examples
inputCopy
4
good
outputCopy
0
good
inputCopy
4
aabc
outputCopy
2
ab
inputCopy
3
aaa
outputCopy
3

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<ctime>
#include<iostream>
#include<algorithm>
#include<map>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<string>
#define ll long long
#define dd double
using namespace std;

int main() {
	ll n;
	cin >> n;
	string s;
	cin >> s;
	char k;
	vector<char> s1;
	for (ll i = 0; i < s.size(); i++) {
		if (s1.empty()) {
			s1.push_back(s[i]);
			k = s[i];
		}
		else {
			if (s1.size() % 2 != 0) {
				if (s[i] == k) {
					continue;
				}
				else {
					s1.push_back(s[i]);
					k = s[i];
				}
			}
			else {
				s1.push_back(s[i]);
				k = s[i];

			}
		}
	}
	if (s1.size() % 2 == 0) {
		cout <<s.size() - s1.size()<< endl;
		for (ll i = 0; i < s1.size(); i++) {
			cout << s1[i];
		}
		cout << endl;
	}
	else {
		cout << s.size() - s1.size() + 1 << endl;
		for (ll i = 0; i < s1.size() - 1; i++) {
			cout << s1[i];
		}
		cout << endl;
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值