题意:

  就是用最少的字符把原字符串补成回文串

解析:

  emm/。。。/网上都是用kmp和后缀数组做的 我没想到这俩的思路。。。emmm。。。

想到了exkmp的  就是原串和逆串匹配一下  注意要保证这个匹配的最大长度 要到原串的结尾

#include <iostream>
#include <cstdio>
#include <sstream>
#include <cstring>
#include <map>
#include <cctype>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <cmath>
#define rap(i, a, n) for(int i=a; i<=n; i++)
#define rep(i, a, n) for(int i=a; i<n; i++)
#define lap(i, a, n) for(int i=n; i>=a; i--)
#define lep(i, a, n) for(int i=n; i>a; i--)
#define rd(a) scanf("%d", &a)
#define rlld(a) scanf("%lld", &a)
#define rc(a) scanf("%c", &a)
#define rs(a) scanf("%s", a)
#define MOD 2018
#define LL long long
#define ULL unsigned long long
#define Pair pair<int, int>
#define mem(a, b) memset(a, b, sizeof(a))
#define _ ios_base::sync_with_stdio(0),cin.tie(0)
//freopen("1.txt", "r", stdin);
using namespace std;
const int maxn = , INF = 0x7fffffff;
int nex[maxn], ex[maxn];
char s1[maxn], s2[maxn];
void get_next(char *s)
{
int i=, j, po, len = strlen(s);
nex[] = len;
while(s[i] == s[i+] && i+ < len)
i++;
nex[] = i;
po = ;
for(int i=; i<len; i++)
{
if(i+nex[i-po] < po + nex[po])
nex[i] = nex[i-po];
else
{
j = po + nex[po] - i;
if(j < ) j = ;
while(i + j < len && s[i+j] == s[j])
j++;
nex[i] = j;
po = i;
}
}
} void get_ex(char *s1, char *s2)
{
int i=, j, po, len1 = strlen(s1), len2 = strlen(s2);
get_next(s2);
while(s1[i] == s2[i] && i < len1 && i < len2)
i++;
ex[] = i;
po = ;
for(int i=; i<len1; i++)
{
if(i + nex[i - po] < po + ex[po])
ex[i] = nex[i-po];
else
{
j = po + ex[po] - i;
if(j < ) j = ;
while(i + j < len1 && j < len2 && s1[i+j] == s2[j])
j++;
ex[i] = j;
po = i;
}
}
} int main()
{
while(~scanf("%s", s1))
{
int len = strlen(s1);
strcpy(s2, s1);
reverse(s2, s2+len);
get_next(s2);
get_ex(s1, s2);
if(ex[] == len)
{
cout<< s1 <<endl;
continue;
}
int maxx = -INF, id;
for(int i=; i<len; i++)
if(maxx < ex[i] && i + ex[i] == len)
{
maxx = ex[i];
id = i;
}
// cout<< maxx <<endl;
if(maxx == )
{
cout<< s1;
cout<< s2+ <<endl;
continue;
}
cout<< s1;
for(int i=id-; i>=; i--)
cout<<s1[i];
cout<<endl; } return ;
}

Extend to Palindrome UVA - 11475(补成回文串)的更多相关文章

  1. UVa 11584 划分成回文串

    https://vjudge.net/problem/UVA-11584 题意: 给出一串字符,把它划分成尽量少的回文串. 思路: 用d[i]表示划分到i时所能划分的最小个数,转移方程为d[i]=mi ...

  2. Palindrome Partitioning LightOJ - 1044(回文串最小分割数,O(n^2)预处理子串是否回文)

    题意:将一个字符串分割成最少的字符串,使得分割出的每个字符串都是回文串.输出最小的分割数. 方法(自己的):先O(n^2)(用某个点或某个空区间开始,每次向左右扩展各一个的方法)处理出所有子串是否回文 ...

  3. [leetcode]131. Palindrome Partitioning字符串分割成回文子串

    Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...

  4. Uva 11584,划分成回文串

    题目链接:https://uva.onlinejudge.org/external/115/11584.pdf 题意: 一个字符串,将它划分一下,使得每个串都是回文串,求最少的回文串个数. 分析: d ...

  5. UVA - 11584 Partitioning by Palindromes(划分成回文串)(dp)

    题意:输入一个由小写字母组成的字符串,你的任务是把它划分成尽量少的回文串,字符串长度不超过1000. 分析: 1.dp[i]为字符0~i划分成的最小回文串的个数. 2.dp[j] = Min(dp[j ...

  6. POJ 3280 Cheapest Palindrome(区间DP求改成回文串的最小花费)

    题目链接:http://poj.org/problem?id=3280 题目大意:给你一个字符串,你可以删除或者增加任意字符,对应有相应的花费,让你通过这些操作使得字符串变为回文串,求最小花费.解题思 ...

  7. UVA11584 划分成回文串

    http://acm.hust.edu.cn/vjudge/contest/view.action?cid=105116#problem/B 紫书275 题意:输入一个字符,最少能划分几个回文串 分析 ...

  8. HDU 4632 Palindrome subsequence(区间dp,回文串,字符处理)

    题目 参考自博客:http://blog.csdn.net/u011498819/article/details/38356675 题意:查找这样的子回文字符串(未必连续,但是有从左向右的顺序)个数. ...

  9. Palindrome - URAL - 1297(求回文串)

    题目大意:RT   分析:后缀数组求回文串,不得不说确实比较麻烦,尤其是再用线段数进行查询,需要注意的细节地方比较多,比赛实用性不高......不过练练手还是可以的.   线段数+后缀数组代码如下: ...

随机推荐

  1. Jupyter安装及运行

    一.安装(来自http://jupyter.org/install) Ctrl+Alt+T(Manjaro系统),打开控制台,su进入root权限 输入如下命令: python3 -m pip ins ...

  2. Gitlab 403 forbidden 并发引起IP被封

    问题 在工作中自搭建的Gitlab.但今天打开页面的时候显示的是空白页面,上面还有一次文本Forbidden. 原因 Gitlab使用rack_attack做了并发访问的限制. 解决方案 将Gitla ...

  3. 【LeetCode算法题库】Day1:TwoSums & Add Two Numbers & Longest Substring Without Repeating Characters

    [Q1] Given an array of integers, return indices of the two numbers such that they add up to a specif ...

  4. 【坚持】Selenium+Python学习之从读懂代码开始 DAY6

    2018/05/23 Python内置的@property装饰器 [@property](https://www.programiz.com/python-programming/property) ...

  5. maven项目的标准目录结构

    maven项目的标准目录结构如下:

  6. python-gevent模块(自动切换io的协程)

    2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 import gevent     def foo() ...

  7. 图-最小生成树算法之Kruskal及其Java实现

    1.Kruskal算法 Kruskal算法基于贪心,因此它追求的是近似最优解,也就是说由Kruskal得出的生成树并不一定是最优解. Kruskal算法求最小生成树的关键在于,每次选取图中权值最小(及 ...

  8. Centos7 Jenkins

    代码上线 持续集成 随时随地将代码合并,这种方法叫做持续集成. 持续集成(CONTINUOUS INTEGRATION,简称CI)持续集成指的是,频繁地(一天多次)将代码集成到主干.它的好处主要有两个 ...

  9. vue ,v-for循环对象,不是深度克隆? 数据改变了但是页面元素没有更新。问题解决

    <div id="app"> <ul > <li v-for="(val,key,idx) in list" > {{key ...

  10. js中使用对象注意

    var myObject = { hello: '你好', world : '世界' } 正常使用 myObject.hello   //输出你好 若引用的属性本身是一个变量: 需要用方括号使用 va ...