本题要考虑字符串本身就存在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. Android Programming: Pushing the Limits -- Chapter 4: Android User Experience and Interface Design

    User Stories Android UI Design 附加资源 User Stories: @.通过写故事来设计应用. @.每个故事只关注一件事. @.不同的故事可能使用相同的组件,因此尽早地 ...

  2. GPS NEMA 0183协议

    转自:http://www.cnblogs.com/xidongs/archive/2011/02/01/1948689.html 一. NMEA0183标准语句(GPS常用语句)$GPGGA例:$G ...

  3. 微软MSMQ消息队列的使用

    首先在windows系统中安装MSMQ 一.MSMQ交互 开发基于消息的应用程序从队列开始.MSMQ包含四种队列类型: 外发队列:消息发送到目的地之前,用它来临时存储消息. 公共队列:在主动目录中公布 ...

  4. Dapper.NET 使用简单举例

    概述 Dapper是.NET下一个micro的ORM,它和Entity Framework或Nhibnate不同,属于轻量级的,并且是半自动的.也就是说实体类都要自己写.它没有复杂的配置文件,一个单文 ...

  5. 垂直时间轴HTML

    1.概述 用时间点来展示事件发生点来代替用table展示一条条数据,能够给人清晰.一目了然能够看清事情发生的过程,UI页面也显示的那么清晰.如何用css+html做出时间轴展示事件点的?先来看看下面的 ...

  6. 【mysql中myisam和innodb的区别】

    单击进入源网页 要点摘要: 1.查看mysql存储引擎的状态mysql> show engines; 2.查看mysql默认的存储引擎mysql> show variables like ...

  7. Filp Game

    Flip Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 25573   Accepted: 11052 题目链接: ...

  8. 攻城狮在路上(壹) Hibernate(十八)--- 管理Hibernate的缓存

    一般Session的缓存被称为Hibernate的第一级缓存,SessionFactory的外置缓存是一个可配置的缓存插件,称为Hibernate的第二级缓存.一.缓存的基本原理: 1.持久化层的缓存 ...

  9. JavaScript - call(this)

    为什么使用call(this), 而不是直接使用(function(){})(); "use strict" function Foo() { (function() { cons ...

  10. Win10 资源文件

    ResourceLoader rl = new ResourceLoader(); DisOutText.Text = rl.GetString("Display"); Resou ...