本题要考虑字符串本身就存在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 中内存映射二进制文件

    内存映射一个文件并不会导致整个文件被读取到内存中. 也就是说,文件并没有被复制到内存缓存或数组中.相反,操作系统仅仅为文件内容保留了一段虚拟内存. 当你访问文件的不同区域时,这些区域的内容才根据需要被 ...

  2. 线性代数 -- Linear Algebra with Applications

    @.如果线性方程组无解,则称该方程组是不相容的(inconsistent). @.如果线性方程组至少存在一个解,则称该方程组是相容的(consistent). @.等价方程组(equivalent s ...

  3. MVC中数据的内部校验

    针对MVC中实例类型的修饰,我们可以采用DataAnnotations类来完成,该类所处的命名空间是System.ComponentModel.DataAnnotations; 一.通过开类型,能够修 ...

  4. 【JAVA集合框架之Map】

    一.概述.1.Map是一种接口,在JAVA集合框架中是以一种非常重要的集合.2.Map一次添加一对元素,所以又称为“双列集合”(Collection一次添加一个元素,所以又称为“单列集合”)3.Map ...

  5. Message Flood

    Message Flood Time Limit: 1500MS Memory limit: 65536K 题目描述 Well, how do you feel about mobile phone? ...

  6. RSA 加解密算法

    与DES不同,RSA算法中,每个通信主体都有两个钥匙,一个公钥一个私钥. 就是有2把钥匙1.使用publicKey可以对数据进行加密2.使用Key才能对数据进行解密单方向传输用公钥加密的数据,只有私钥 ...

  7. flume-ng 集群搭脚本

    #!/bin/bash # author: xirong # date : -- ##### 搭建 flume 集群的脚本 # 注意: # . 需要 jdk7 环境,如果没有 Java 环境,请配置 ...

  8. C# IIS应用程序池辅助类 分类: C# Helper 2014-07-19 09:50 249人阅读 评论(0) 收藏

    using System.Collections.Generic; using System.DirectoryServices; using System.Linq; using Microsoft ...

  9. golang json 包简单分析

    首先上代码: func main() { b := true a1, _ := json.Marshal(b) a2, _ := Marshal(b) fmt.Println(string(a1)) ...

  10. 第十四篇:在SOUI中使用定时器

    前言 定时器是win32编程中常用的制作动画效果的手段.在Win32编程中,可以使用::SetTimer来创建定时器,定时器消息会被会发到调用SetTimer时指定的HWND. 在SOUI中一般来说只 ...