Codeforces Round #560 (Div. 3) C题

题目网址:http://codeforces.com/contest/1165/problem/C

题目大意:给出一个串,length是n,经过删除字符可得到一个子串,且子串满足,a[1] ! = a[2],a[3] ! = a[4]..........问最长的子串是多少?输出删除字符的个数和子串。

题解:直接历遍,注意到子串的性质,设子串长度是k,如果k是偶数,要考虑字符相同的影响,因为比如  x y t t d,从左向右扫的时候,第一个 ‘ t ’不能成为子串中的字符。

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 const int maxn=2e5+7;
 4 char str[maxn],ans[maxn];
 5 int main()
 6 {
 7     int n,k;
 8     cin>>n;
 9     int tot=0;
10     scanf("%s",str+1);
11     for(int i=1;i<=n-1;i++) {
12         if(str[i]==str[i+1]&&tot%2==0) continue;
13         ans[++tot]=str[i]; 
14     }
15     ans[++tot]=str[n];
16     if(tot&1) tot--;
17     cout<<n-tot<<endl;
18     for(int i=1;i<=tot;i++) printf("%c",ans[i]);
19     return 0;
20 } 
View Code

猜你喜欢

转载自www.cnblogs.com/duxing201806/p/10885881.html