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 分析:后缀数组求回文串,不得不说确实比较麻烦,尤其是再用线段数进行查询,需要注意的细节地方比较多,比赛实用性不高......不过练练手还是可以的. 线段数+后缀数组代码如下: ...
随机推荐
- hashCode及HashMap中的hash()函数
一.hashcode是什么 要理解hashcode首先要理解hash表这个概念 1. 哈希表 hash表也称散列表(Hash table),是根据关键码值(Key value)而直接进行访问的数据结构 ...
- 我是如何将页面加载时间从6S降到2S的?
写在前面 生活在信息爆炸的今天,我们每天不得不面对和过滤海量的信息--无疑是焦躁和浮动的,这就意味着用户对你站点投入的时间可能是及其吝啬的(当然有一些刚需站点除外). 如何给用户提供迅速的响应就显得十 ...
- springboot 前后端分离开发 从零到整(二、邮箱注册)
spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver username: root password: 123456 url: ...
- C程序员必读的 3 本书
C程序员必读的 3 本书 原创: Martin 老师 公众号:零基础零障碍学习C语言 勿用质疑,今天来看这篇文章的朋友都是准备学好C语言的朋友,大家想学好C语言的迫切性,就好比Martin老师等着元 ...
- 5. 使用Flask蓝图(blueprint)
一直到现在都没有怎么写代码,可能更得比较慢. 作业回顾 先来看一下文章4的作业吧,使用logbook的时候,遇到了时区不对的情况.那么我们怎么去解决这个问题呢? 实际上logbook默认采用的是世界标 ...
- ELK环境搭建
ELK环境搭建 1. Virtualbox/Vagrant安装 41.1. Virtualbox安装 41.2. Vagrant安装 41.2.1. 简述 41.2.2. Vagrant box 41 ...
- 半年收入超2亿RMB 独立游戏开发者的艰苦创业路
一款叫做<监狱建筑师>的模拟经营游戏,目前在Steam平台获得了3000万美元(近2亿元)以上的收入.这款游戏由英国独立工作室Introversion Software发布,而团队最困难的 ...
- 吴恩达 Deep learning 第二周 神经网络基础
逻辑回归代价函数(损失函数)的几个求导特性 1.对于sigmoid函数 2.对于以下函数 3.线性回归与逻辑回归的神经网络图表示 利用Numpy向量化运算与for循环运算的显著差距 import nu ...
- [奇葩问题] Error Domain=NSURLErrorDomain Code=-1003
问题描述: 新上线的产品,ios同事拿着一串报错来找我,日志如下:err =Error Domain=NSURLErrorDomain Code=-1003 "未能找到使用指定主机名的服务器 ...
- 测试效率 timeit cProfile
timeit使用 def f1(lIn): l1 = sorted(lIn) # O(nlogn) C语言的 l2 = [i for i in l1 if i<0.5] # O(n) retur ...