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 ss, you have to delete minimum number of characters from this string so that it becomes good.
Input
The first line contains one integer nn (1≤n≤2⋅1051≤n≤2⋅105) — the number of characters in ss.
The second line contains the string ss, consisting of exactly nn lowercase Latin letters.
Output
In the first line, print one integer kk (0≤k≤n0≤k≤n) — the minimum number of characters you have to delete from ss to make it good.
In the second line, print the resulting string ss. If it is empty, you may leave the second line blank, or not print it at all.
Examples
Input
4 good
Output
0 good
Input
4 aabc
Output
2 ab
Input
3 aaa
Output
3
#include <iostream> #include <algorithm> #include <cstdio> #include <string> #include <cstring> #include <cstdlib> #include <map> #include <vector> #include <set> #include <queue> #include <stack> #include <cmath> // #define lson rt<<1, l, m #define rson rt<<1|1, m+1, r // #define fi first #define se second #define pb push_back #define pq priority_queue<int> #define ok return 0; #define os(str) cout<<string(str)<<endl; #define gcd __gcd #define mem(s,t) memset(s,t,sizeof(s)) #define debug(a,n) for(int i=0;i<n;i++) cout<<a[i]<<" "; cout<<endl; #define debug1(a,n) for(int i=1;i<=n;i++) cout<<a[i]<<" "; cout<<endl; #define debug02(a,n,m) for(int i=0;i<n;i++) {for(int j=0;j<m;j++) cout<<a[i][j]<<" "; cout<<endl; } #define read11(a,k) for (int i = 1; i <= (int)(k); i++) {cin>>a[i];} #define read02(a,n,m) for(int i=0;i<n;i++) {for(int j=0;j<m;j++) cin>>a[i][j] ; } #define TLE std::ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cout.precision(10); using namespace std; inline void NO() { cout<<"NO"<<endl; } inline void YES() { cout<<"YES"<<endl; } const int mxn = 2e5+10; #define oi(x) cout<<x<<endl; #define rep(k) for (int i=0;i<n;i++) #define rep1(j,k) for (int i=j;i<=k; i++) #define per(j,k) for (int i=j;i>=k; i--) #define per(k) for (int i=k-1;i>=0;i--) string str,ch; int n; int main() { int n; while(cin>>n) { ch=""; cin>>str; int j; for(int i=0; i<n;) { if( str[i]!=str[i+1] && i+1<n ) { ch+=str[i]; ch+=str[i+1]; i+=2; } else { for(j=i+2;j<n && j<n;) { if(str[i]!=str[j]) { ch+=str[i]; ch+=str[j]; i=j+1; j++; break; } else j++; } i=j; } } cout<<str.size()-ch.size()<<endl; cout<<ch<<endl; } ok; }
原文地址:https://www.cnblogs.com/Shallow-dream/p/11621443.html