本题要考虑字符串本身就存在tandem,

如测试用例

aaaaaaaaabbb

3

输出结果应该是8而不是6,因为字符串本身的tanderm时最长的

故要考虑字符串本身的最大的tanderm和添加k个字符后最大的tanderm

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <set> using namespace std; int maxTandem(string& str){
for(int inteval = str.length()/; inteval>=; --inteval){
for(int i = ; i <=str.length()-*inteval; ++i){
bool flag = false;
for(int j = i,k=i+inteval; j < i+inteval; j++,k++){
if(str[j] != str[k]){
flag = true;
break;
}
}
if(!flag) return inteval;
}
}
return ;
} int main(){
string str;
int k;
cin >> str >> k;
int len = str.length();
if(k>=len){
cout<<((k+len)% ? k+len-:k+len)<<endl;
}else{
int newlen = len+k, startIndex=newlen-newlen/*, p =newlen/;
for( p = newlen/; p> k; -- p){
int startIndex=newlen-p*;
bool flag = false;
for(int i = startIndex, j = startIndex+p; i < min(len,startIndex+p) && j < len; ++ i, ++ j){
if(str[i] != str[j]){
flag = true;
break;
}
}
if(!flag) break;
}
cout<<*max(p,maxTandem(str))<<endl;
}
}

本题将上面两个部分合并,即在字符串s后面添加k个‘?’,再对整个字符串搜索

#include <iostream>
#include <vector>
#include <string>
#include <algorithm> using namespace std; int main(){
string s;
int k;
cin >> s >> k;
int slen = s.length();
s+=string(k,'?');
int len = s.length()/, interval = len;
for(interval = len; interval >=; interval--){
bool flag = false;
for(int p = ; p<= s.length()-*interval; ++ p){
flag = false;
for(int i = p, j = p+interval; i < min(slen,p+interval) && j < slen; ++ i, ++ j){
if(s[j]!= '?' && s[i]!=s[j] ){ flag = true;break;}
}
if(!flag) break;
}
if(!flag) break;
}
cout<<*interval<<endl;
}

Codeforces Round #253 (Div. 2) B - Kolya and Tandem Repeat的更多相关文章

  1. Codeforces Round 253 (Div. 2)

    layout: post title: Codeforces Round 253 (Div. 2) author: "luowentaoaa" catalog: true tags ...

  2. Codeforces Round #253 (Div. 1) (A, B, C)

    Codeforces Round #253 (Div. 1) 题目链接 A:给定一些牌,然后如今要提示一些牌的信息,要求提示最少,使得全部牌能够被分辨出来. 思路:一共2^10种情况,直接暴力枚举,然 ...

  3. Codeforces Round #324 (Div. 2) B. Kolya and Tanya 快速幂

    B. Kolya and Tanya Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/584/pro ...

  4. Codeforces Round #253 (Div. 2) D. Andrey and Problem

    关于证明可以参考题解http://codeforces.com/blog/entry/12739 就是将概率从大到小排序然后,然后从大到小计算概率 #include <iostream> ...

  5. Codeforces Round #253 (Div. 2) D题

    题目大意是选出一个其他不选,问问最大概率: 刚开始想到DP:F[I][J][0]:表示从 前I个中选出J个的最大值, 然后对于F[I][J][1]=MAX(F[I-1][J][1],F[I-1][J- ...

  6. Codeforces Round #253 (Div. 1) A. Borya and Hanabi 暴力

    A. Borya and Hanabi Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/442/p ...

  7. Codeforces Round #253 (Div. 2) A. Anton and Letters

    题目很简单,只需要注意带空格的输入用getline即可 #include <iostream> #include <vector> #include <algorithm ...

  8. Codeforces Round #253 (Div. 2), problem: (B)【字符串匹配】

    简易字符串匹配,题意不难 #include <stdio.h> #include <string.h> #include <math.h> #include < ...

  9. Codeforces Round #253 (Div. 1) B. Andrey and Problem

    B. Andrey and Problem time limit per test 2 seconds memory limit per test 256 megabytes input standa ...

随机推荐

  1. python实现支持并发、断点续传的Ftp程序

    一.要求 1.用户md5认证 2.支持多用户同时登陆(并发) 3.进入用户的命令行模式,支持cd切换目录,ls查看目录子文件 4.执行命令(ipconfig) 5.传输文件: a.支持断点续传 b.传 ...

  2. sdut1598 周游列国【简单模拟题】

    周游列国 Time Limit: 1000ms   Memory limit: 32768K  有疑问?点这里^_^ 题目描述 题目链接:http://acm.sdut.edu.cn/sdutoj/p ...

  3. I-number

    以下是真坑爹题目: 此题必须输出前导零,否则永远a不了 I-number Time Limit: 5000MS Memory limit: 65536K 题目描述 The I-number of x ...

  4. Java程序员要求具备的10项技能

    1.语法:必须比较熟悉,在写代码的时候IDE的编辑器对某一行报错应该能够根据报错信息知道是什么样的语法错误并且知道任何修正. 2.命令:必须熟悉JDK带的一些常用命令及其常用选项,命令至少需要熟悉:a ...

  5. 验证备份前设置CONFIGURE CONTROLFILE AUTOBACKUP ON/OFF; 的区别

    关于rman的,环境: oracle 10.2.0 rman nocatalog方式 1.首先设置 CONFIGURE CONTROLFILE AUTOBACKUP ON; 然后进行数据库全备份 RM ...

  6. PHP商品秒杀计时实现(解决大流量方案)

    PHP商品秒杀功能我们多半以整点或时间点为例子,这样对于php来说处理不复杂,但有一个问题就是如果流量大要如何来处理,下面我们一起来看看解决办法. 要求要有小时分钟秒的实时倒计时的显示,用户端修改日期 ...

  7. phpcms调用一级栏目和二级栏目

    {loop subcat(,,,$siteid) $r}  {php $num++} <strong><a href=} <br /> {elseif $n!=$c} | ...

  8. [Tools] Eclipse更改类注释自动生成模板

    [背景] 使用之中发现一些eclipse使用的小技巧,记录下来供以后查阅   由于机器是老婆的,创建新类的时候或者生成注释的时候全都是她的名字,避免弄混,需要设置一下: 设置创建新类时自动生成类或方法 ...

  9. RTP timestamp与帧率及时钟频率的关系

    转自:http://blog.csdn.net/jasonhwang/article/details/7316128 RTP timestamp是用时钟频率(clock rate)计算而来表示时间的. ...

  10. 深入分析JavaWeb 技术内幕

    1,通过浏览器请求一个资源,会发生以下几种过程 1) http的解析过程,包括对于http请求头和响应头中指令(控制用户浏览器的渲染行为和 服务器的执行逻辑)的解析 2)DNS的解析过程(根据域名获取 ...