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<cstring>
#include<algorithm>
#include<cmath>
#include<iostream>
#include<string>
using namespace std;
int main() {
ios::sync_with_stdio(false);
int n;
char a[200005], b[200005];
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
if (n == 1) {
cout << '0' << endl;
return 0;
}
int k = 1;
int x = 1;
while (true) {
if (x >= n)break;
for (int j = x + 1; j <= n; j++) {
if (a[j] != a[x]) {
b[k] = a[x];
b[k + 1] = a[j];
k += 2;
x = j + 1;
break;
}
if (j == n)x = n;
}
}
cout << n - k + 1 << endl;
for (int i = 1; i < k; i++)cout << b[i];
return 0;
}