Extend to Palindrome UVA - 11475(补成回文串)
题意:
就是用最少的字符把原字符串补成回文串
解析:
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(补成回文串)的更多相关文章
- UVa 11584 划分成回文串
https://vjudge.net/problem/UVA-11584 题意: 给出一串字符,把它划分成尽量少的回文串. 思路: 用d[i]表示划分到i时所能划分的最小个数,转移方程为d[i]=mi ...
- Palindrome Partitioning LightOJ - 1044(回文串最小分割数,O(n^2)预处理子串是否回文)
题意:将一个字符串分割成最少的字符串,使得分割出的每个字符串都是回文串.输出最小的分割数. 方法(自己的):先O(n^2)(用某个点或某个空区间开始,每次向左右扩展各一个的方法)处理出所有子串是否回文 ...
- [leetcode]131. Palindrome Partitioning字符串分割成回文子串
Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...
- Uva 11584,划分成回文串
题目链接:https://uva.onlinejudge.org/external/115/11584.pdf 题意: 一个字符串,将它划分一下,使得每个串都是回文串,求最少的回文串个数. 分析: d ...
- UVA - 11584 Partitioning by Palindromes(划分成回文串)(dp)
题意:输入一个由小写字母组成的字符串,你的任务是把它划分成尽量少的回文串,字符串长度不超过1000. 分析: 1.dp[i]为字符0~i划分成的最小回文串的个数. 2.dp[j] = Min(dp[j ...
- POJ 3280 Cheapest Palindrome(区间DP求改成回文串的最小花费)
题目链接:http://poj.org/problem?id=3280 题目大意:给你一个字符串,你可以删除或者增加任意字符,对应有相应的花费,让你通过这些操作使得字符串变为回文串,求最小花费.解题思 ...
- UVA11584 划分成回文串
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=105116#problem/B 紫书275 题意:输入一个字符,最少能划分几个回文串 分析 ...
- HDU 4632 Palindrome subsequence(区间dp,回文串,字符处理)
题目 参考自博客:http://blog.csdn.net/u011498819/article/details/38356675 题意:查找这样的子回文字符串(未必连续,但是有从左向右的顺序)个数. ...
- Palindrome - URAL - 1297(求回文串)
题目大意:RT 分析:后缀数组求回文串,不得不说确实比较麻烦,尤其是再用线段数进行查询,需要注意的细节地方比较多,比赛实用性不高......不过练练手还是可以的. 线段数+后缀数组代码如下: ...
随机推荐
- 英文样式教师求职简历免费word模板
10款英文样式教师求职简历免费word模板,也可用于其他专业和职业,个人免费简历模板,个人简历表免费,个人简历表格. 声明:该简历模板仅用于个人欣赏使用,请勿用于商业用途,谢谢. 下载地址:百度网盘, ...
- leetcode_11. Container With Most Water
leetcode_11. Container With Most Water 一,问题: Given n non-negative integers a1, a2, ..., an, where ea ...
- 3、ObjectARX开发创建直线、圆、圆弧和修改对象属性
一.本节课程 Arx二次开发创建直线.圆.圆弧和修改对象属性 二.本节要讲解的知识点 1.如何应用C++ ARX二次开发创建直线. 2.如何应用C++ ARX二次开发创建圆. 3.如何应用C++ AR ...
- Controller层@PathVariable使用
@PathVariable 映射 URL 绑定的占位符 带占位符的 URL 是 Spring3.0 新增的功能,该功能在SpringMVC 向 REST 目标挺进发展过程中具有里程碑的意义通过 @Pa ...
- SSM搭项目报错:HTTP Status 400 – Bad Request
具体报错如下: Type Status Report Description The server cannot or will not process the request due to some ...
- python虚拟环境管理之virtualenv,virtualenvwrapper,pipenv,conda
虚拟环境的作用 使python环境拥有独立的包,避免污染原本的python环境.为不同的项目创建不同的环境可以避免安装的库过于庞大和相互干扰. 例如你想在同一台机器上开发用python2和python ...
- 利用jsencrypt 做非对称加密
1.生成 private key openssl genrsa -out rsa_1024_priv.pem 1024 2.生成public key openssl rsa -pubout -in r ...
- hadoop之定制自己的Partitioner
partitioner负责shuffle过程的分组部分,目的是让map出来的数据均匀分布在reducer上,当然,如果我们不需要数据均匀,那么这个时候可以自己定制符合要求的partitioner. 下 ...
- uiimageview 的 animation 动画
NSMutableArray *meiArr = [NSMutableArray arrayWithCapacity:4]; for (int i = 0; i < 4; i++) { NSSt ...
- lastlog命令详解
基础命令学习目录首页 原文链接:https://www.cnblogs.com/qiyebao/p/4331078.html last 显示所有用户最后登录信息(会显示系统用户) last -u 50 ...