首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >Codeforces Round #505 C. Plasticine zebra(思维)

Codeforces Round #505 C. Plasticine zebra(思维)

作者头像
Ch_Zaqdt
发布于 2019-01-11 11:08:20
发布于 2019-01-11 11:08:20
6870
举报
文章被收录于专栏:Zaqdt_ACMZaqdt_ACM

题目链接:http://codeforces.com/contest/1025/problem/C

       题意是输入一个只有'w','b'的字符串,可以对他的任意位置切割成两个子串,切割后的右边的子串翻转后再和左边的子串拼起来会得到一个新的字符串,操作次数不限,问能得到的字符串中wb交替出现的最大的长度是多少。

       其实就是求这个字符串首尾相连成环后的wb交替的最大长度。在原字符串上再往后加一个原字符串(str += str),这样扫一遍就相当于进行了成环的操作,注意如果最大长度大于。str.length()的话应该输出str.length();(当然不能超过字符串本身的长度啊)...


AC代码:

代码语言:javascript
复制
#include <bits/stdc++.h>
using namespace std;
string str;

int main()
{
  cin>>str;
  int len = str.length();
  if(len == 1){
    puts("1");
    return 0;
  }
  str += str;
  int sum = 1;
  int ans = -1;
  for(int i=1;i<len*2;i++){
    if(str[i] != str[i-1])sum++;
    else{
      ans = max(ans,sum);
      sum = 1;
    }
  }
  ans = max(ans,sum);
  if(ans > len)ans = len;
  cout<<ans<<endl;
  return 0;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018年08月20日,如有侵权请联系 cloudcommunity@tencent.com 删除
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档