本题要考虑字符串本身就存在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. zip 压缩文件 unzip查看zip压缩包内的内容

    [root@GitLab tmp]# zip -r new.zip ./*  adding: gitlab_key_file20161001-2668-1eu44mv (deflated 15%)  ...

  2. TCP 四次握手

    TCP协议中的三次握手和四次挥手(图解) http://blog.csdn.net/whuslei/article/details/6667471/

  3. Flex导出excel报表

    sheetToExcel.java 1 package tree; 2 import java.io.BufferedInputStream; 3 import java.io.File; 4 imp ...

  4. Win10 创建应用程序包及部署

    https://msdn.microsoft.com/zh-cn/library/windows/apps/xaml/hh454036.aspx https://msdn.microsoft.com/ ...

  5. PHPCMS-V9 获取一级栏目、二级栏目、三级栏目

    <!-- 获取一级栏目 -->{pc:content action=" siteid="$siteid" order="listorder ASC& ...

  6. QQ互联OAuth

    /** * QQ互联 oauth * @author dyllen * */ class Oauth { //取Authorization Code Url const PC_CODE_URL = ' ...

  7. 获得H.264视频分辨率的方法

    转自:http://www.cnblogs.com/likwo/p/3531241.html 在使用ffmpeg解码播放TS流的时候(例如之前写过的UDP组播流),在连接时往往需要耗费大量时间.经过d ...

  8. linux 读写锁应用实例

    转自:http://blog.csdn.net/dsg333/article/details/22113489 /*使用读写锁实现四个线程读写一段程序的实例,共创建了四个新的线程,其中两个线程用来读取 ...

  9. Matlab中如何将(自定义)函数作为参数传递给另一个函数

    假如我们编写了一个积分通用程序,想使它更具有通用性,那么可以把被积函数也作为一个参数.在c/c++中,可以使用函数指针来实现上边的功能,在matlab中如何实现呢?使用函数句柄--这时类似于函数指针的 ...

  10. 【项目经验】navicat工具 SQLServer数据库迁移MySQL

    新近领了一个任务,就是把SQL Server的数据库迁移到My Sql上,经过查资料,圆满完成任务.分享一下流程. 1.首先,在自己的My Sql数据库上新建一个数据库. 2.打开新建的My Sql数 ...