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

析:先用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. PCBA 的收货要求记录

    PCBA 的收货要求记录 性能 功能性测试 外观 标识 需要可以识别的料号(客户料号或货号) 贴片 元件焊点饱满 元件参数统一 后焊 插件焊盘饱满 插件焊盘不可以有小孔 焊盘不可以有漏铜上锡均匀 体积 ...

  2. Aix之 xmanager 2.0连接AIX服务器

    xmanager连接AIX服务器可以分为两种情况:1.连接IBM服务器,使用远程桌面功能进行系统维护.要求这台服务器已经安装了图形桌面,如CDE等,并启动到图形界面.在xmanager中的Xbrows ...

  3. 洛谷 1365 WJMZBMR打osu! / Easy

    题目:https://www.luogu.org/problemnew/show/P1365 大水题.记录一下o的期望长度. 关键是(x+1)^2=x^2+2*x+1. #include<ios ...

  4. jq添加和移除事件的方法,prop和attr

    会在写条件判断的时候遇到,今天在判断没有剩余产品的时候,移除事件.当有产品的时候添加事件: 移除onClick事件: $("a").removeAttr("onclick ...

  5. ORACLE常识

    1. ORACLE中查看表中的外键来源于哪些表 select cl.table_name from user_cons_columns cl left join user_constraints c ...

  6. FPGA市场潜力有几多?

    FPGA市场未来成长潜力 本文来源:DIGITIMES 2014年FPGA市场规模为52.7亿美元,据Green Mountain Outlook报导,研调机构Global Market Insigh ...

  7. 分布式缓存系统 Memcached 半同步/半异步模式

    在前面工作线程初始化的分析中讲到Memcached采用典型的Master_Worker模式,也即半同步/半异步的高效网络并发模式.其中主线程(异步线程)负责接收客户端连接,然后分发给工作线程,具体由工 ...

  8. Maven使用阿里云镜像

    Maven确实是个好用的东西,不过在国内的话下载速度不够快,推荐使用阿里云的镜像,配置方法还是比较简单,这里是全局的配置文件 settings.xml中的内容: <settings xmlns= ...

  9. PyYAML和configparser模块讲解

    Python也可以很容易的处理ymal文档格式,只不过需要安装一个模块,参考文档:http://pyyaml.org/wiki/PyYAMLDocumentation ymal主要用于配置文件. Co ...

  10. python学习笔记(四):函数

    一.函数是什么? 函数一词来源于数学,但编程中的「函数」概念,与数学中的函数是有很大不同的,编程中的函数在英文中也有很多不同的叫法.在BASIC中叫做subroutine(子过程或子程序),在Pasc ...