题意:给定上一个串,让你在后面添加一些字符,使得这个串成为一个回文串。

析:先用manacher算法进行处理如果发现有字符匹配超过最长的了,结束匹配,答案就是该字符前面那个长度加上该串原来的长度。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e16;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1e6 + 10;
const int mod = 1e9 + 7;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
} char t[maxn], s[maxn*2];
int p[maxn*2]; int main(){
int T; cin >> T;
for(int kase = 1; kase <= T; ++kase){
scanf("%s", t);
memset(p, 0, sizeof p);
s[0] = '$'; s[1] = '#';
int cnt = 1;
n = 0;
for(int i = 0; t[i]; ++i, ++n){
s[++cnt] = t[i];
s[++cnt] = '#';
}
s[++cnt] = 0;
int mx = 0, id = 0;
int ans = 0;
for(int i = 1; s[i]; ++i){
p[i] = mx > i ? min(p[2*id-i], mx-i) : 1;
while(s[p[i]+i] == s[i-p[i]]) ++p[i];
if(p[i] + i > mx){
mx = p[i] + i;
id = i;
}
if(mx + 1 >= cnt){
ans = (i - p[i]) / 2 + n;
break;
}
}
printf("Case %d: %d\n", kase, ans);
}
return 0;
}

  

LightOJ 1258 Making Huge Palindromes (Manacher)的更多相关文章

  1. lightOJ 1258 Making Huge Palindromes(KMP)

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1258 就是求逆串和原串的匹配长度 答案就是原串长度的2倍减去匹配长度即可 第一次我将原 ...

  2. LightOJ 1258 Making Huge Palindromes(KMP)

    题意 给定一个字符串 \(S\) ,一次操作可以在这个字符串的右边增加任意一个字符.求操作之后的最短字符串,满足操作结束后的字符串是回文. \(1 \leq |S| \leq 10^6\) 思路 \( ...

  3. Making Huge Palindromes LightOJ - 1258

    题目链接:LightOJ - 1258 1258 - Making Huge Palindromes   PDF (English) Statistics Forum Time Limit: 1 se ...

  4. cf245H Queries for Number of Palindromes (manacher+dp)

    首先马拉车一遍(或者用hash),再做个前缀和处理出f[i][j]表示以j为右端点,左端点在[i,j]的回文串个数 然后设ans[i][j]是[i,j]之间的回文串个数,那就有ans[i][j]=an ...

  5. POJ - 3376 Finding Palindromes manacher+字典树

    题意 给n个字符串,两两拼接,问拼接后的\(n\times n\)个字符串中有多少个回文串. 分析 将所有正串插入字典树中,马拉车跑出所有串哪些前缀和后缀为回文串,记录位置,用反串去字典树中查询,两字 ...

  6. D - 楼下水题(kmp+Manacher)

    D - 楼下水题 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit Statu ...

  7. manacher算法专题

    一.模板 算法解析:http://www.felix021.com/blog/read.php?2040 *主要用来解决一个字符串中最长回文串的长度,在O(n)时间内,线性复杂度下,求出以每个字符串为 ...

  8. LightOJ 1033 Generating Palindromes(dp)

    LightOJ 1033  Generating Palindromes(dp) 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid= ...

  9. 【SPOJ】NUMOFPAL - Number of Palindromes(Manacher,回文树)

    [SPOJ]NUMOFPAL - Number of Palindromes(Manacher,回文树) 题面 洛谷 求一个串中包含几个回文串 题解 Manacher傻逼题 只是用回文树写写而已.. ...

随机推荐

  1. SEO优化---10分钟学会建立高转化率的网站关键词库

    想要优化好一个网站,行业的分析,以及关键词的挖掘是必要的,有一定的关键词排名了,但是转化率和流量方面却很不理想这种情况大部分是只注重了有指数的关键词排名,而忽略了长尾关键词和一些没有指数但是可以带来巨 ...

  2. Windows下查看什么进程占用文件

    任务管理器→性能Tab→资源管理器→CPU→关联的句柄后面的检索框中录入文件名(关键文件夹即可). 比如我的是在删除tomcat下面的WEB-INF文件出现问题:就输入WEB-INF:mygod,发现 ...

  3. poj 3415 Common Substrings——后缀数组+单调栈

    题目:http://poj.org/problem?id=3415 因为求 LCP 是后缀数组的 ht[ ] 上的一段取 min ,所以考虑算出 ht[ ] 之后枚举每个位置作为右端的贡献. 一开始想 ...

  4. (转)java.lang.RuntimeException: Missing type parameter

    java.lang.RuntimeException: Missing type parameter (2015-04-07 14:35:51)   分类: 技术 程序中用到了gson的new typ ...

  5. git 查看、创建、切换、删除、重命名和推送分支

    1.查看本地所有分支:前面有 “*” 的是当前所处的分支 $ git branch test-A * test-B 2.查看本地和远程服务器的所有分支: $ git branch -a test-A ...

  6. webstorm设置修改文件后自动编译并刷新浏览器页面

    转载:http://www.cnblogs.com/ssrsblogs/p/6155747.html 重装了 webstorm ,从10升级到了2016 一升不要紧,打开老项目,开启webpakc-d ...

  7. find 使用指南

    find 使用方法整理 -name  按照文件名查找文件. -perm  按照文件权限来查找文件. -user  按照文件属主来查找文件. -group  按照文件所属的组来查找文件. - n表示文件 ...

  8. 今天去python官网下载包安装的时候的问题记录

    去官网下载了 tar压缩包 放到了site-packages下解压 然后使用 python setup.py install 安装 安装完后,所要引用的模块文件居然还在解压出来的压缩文件里面,导致无法 ...

  9. FTP,FTPS,FTPS与防火墙

    昨天搭建了一台FTPS服务器,过程中学习了很多不清楚的知识点,还有遇到的问题,记录一下. (大部分内容汇集.整理自网络) 一. 关于FTP传输模式 众所周知,FTP传输有两种工作模式,Active M ...

  10. 第15届浙江省赛 E LIS

    LIS Time Limit: 1 Second      Memory Limit: 65536 KB      Special Judge DreamGrid is learning the LI ...