• 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
  • 相关阅读:
    第二周作业修改+
    第三周作业
    第二周作业修改
    第三次作业
    第二次作业
    获奖感想
    最后的作业
    14周作业
    第七周作业
    第六周作业
  • 原文地址:https://www.cnblogs.com/duxing201806/p/10885881.html
Copyright © 2020-2023  润新知