题意:问你最少加几个字母使所给串变成回文串。

思路:一开始打算将正序和逆序都hash,然后用提取前缀后缀的方法来找,但是RE了,debug失败遂弃之。后来发现可以直接hash,一边hash一边比较。我们只需找出正序hash值和逆序hash相同的最长串就是最长回文子串。

代码:

#include<stack>
#include<vector>
#include<queue>
#include<set>
#include<cstring>
#include<string>
#include<sstream>
#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#define ll long long
#define ull unsigned long long
using namespace std;
const int maxn = +;
const int seed = ;
const int MOD = ;
const int INF = 0x3f3f3f3f;
char s[maxn];
int main(){
while(scanf("%s", s + ) != EOF){
int len = strlen(s + );
ull suf = ,pre = ,bit = ,Max = ;
for(int i = len; i >= ; i--){
pre = pre * seed + s[i]; //倒序前缀
if(i != len) bit *= seed;
suf = suf + bit * s[i]; //正序后缀
if(pre == suf){
Max = len - i + ;
}
}
for(int i = ; i <= len; i++)
printf("%c", s[i]);
for(int i = len - Max; i >= ; i--)
printf("%c", s[i]);
printf("\n");
}
return ;
}
/*
aaaa
abba
amanaplanacanal
xyz
*/

UVA 11475 Extend to Palindrome(hash)题解的更多相关文章

  1. UVA 11475 Extend to Palindrome hash

    题意: 给出一个字符串,让你往后添加最少的字符,使其成为回文串. 分析: 题目就相当于求后缀字符串为回文串的最长长度,判断回文串要O(n)时间,直接判断肯定不行.我们从后往前枚举,每次字符串与上一个字 ...

  2. uva 11475 - Extend to Palindrome(KMP)

    option=com_onlinejudge&Itemid=8&category=506&page=show_problem&problem=2470" ta ...

  3. UVA 11475 Extend to Palindrome(后缀数组+ST表)

    [题目链接] http://acm.hust.edu.cn/vjudge/problem/27647 [题目大意] 给出一个字符串,要求在其后面添加最少的字符数,使得其成为一个回文串.并输出这个回文串 ...

  4. UVA - 11475 Extend to Palindrome —— 字符串哈希 or KMP or 后缀数组

    题目链接:https://vjudge.net/problem/UVA-11475 题意: 给出一个字符串,问在该字符串后面至少添加几个字符,使得其成为回文串,并输出该回文串. 题解: 实际上是求该字 ...

  5. UVA - 11475 Extend to Palindrome (后缀数组)

    Your task is, given an integer N, to make a palidrome (word that reads the same when you reverse it) ...

  6. UVa 11475 - Extend to Palindrome

    題目:給你一個字符串,在後面拼接一部分使得它變成回文串,使得串最短.輸出這個回文串. 分析:KMP,dp.這裡利用KMP算法將串和它的轉置匹配,看結束時匹配的長度就可以. 因為串比较長.使用KMP比较 ...

  7. UVA 11475 Extend to Palindrome (kmp || manacher || 后缀数组)

    题目链接:点击打开链接 题意:给你一个串,让你在串后面添加尽可能少的字符使得这个串变成回文串. 思路:这题可以kmp,manacher,后缀数组三种方法都可以做,kmp和manacher效率较高,时间 ...

  8. 区间DP UVA 10739 String to Palindrome

    题目传送门 /* 题意:三种操作,插入,删除,替换,问最少操作数使得字符串变成回文串 区间DP:有一道类似的题,有点不同的是可以替换,那么两端点不同的时候可以替换掉一个后成回文, 即dp[j+1][k ...

  9. Extend to Palindrome UVA - 11475(补成回文串)

    题意: 就是用最少的字符把原字符串补成回文串 解析: emm/.../网上都是用kmp和后缀数组做的 我没想到这俩的思路...emmm... 想到了exkmp的  就是原串和逆串匹配一下  注意要保证 ...

随机推荐

  1. Ubuntu 14.04.02 安装openvswitch-2.3.1

    Open vSwitch安装 安装好操作系统 # lsb_release -a LSB Version: core-2.0-amd64:core-2.0-noarch:core-3.0-amd64:c ...

  2. 设计模式之工厂方法模式(Java实现)

    “我先来”,“不,老公,我先!”.远远的就听到几个人,哦不,是工厂方法模式和抽象工厂模式俩小夫妻在争吵,尼妹,又不是吃东西,谁先来不都一样(吃货的世界~).“抽象工厂模式,赶紧的自我介绍,工厂方法模式 ...

  3. LeetCode——Move Zeroes

    Description: Given an array nums, write a function to move all 0's to the end of it while maintainin ...

  4. 腾讯云大数据套件Hermes-MR索引插件使用总结

    版权声明:本文由王亮原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/121 来源:腾云阁 https://www.qclou ...

  5. binlog分析方法

    线上处理问题的时候经常遇到业务说写入了但是数据不对,这种情况八成是业务改了之后又改过来了.但是我们需要证据,下面我们就说说简单的binlog分析 工具:mysqlbinlog #-vvv显示明文sql ...

  6. checkbox的name与JavaBean的交互时发现的一个现象

    一个页面: <form action="reg.jsp" method="post"> <ul> <li>算法选择</ ...

  7. Mongo Rocks 在SSD盘上表现优秀,专门为SSD盘的优化

    2017 · MongoDB深圳用户组大会全记录 | MongoDB中文社区http://www.mongoing.com/2017/04/24/mongodb-shenzhen-user-group ...

  8. HTTP监视器charles入门使用教程分享---http/s packet monitors---ubuntu installation

    charles --usage http://www.cnblogs.com/chenlogin/p/5849471.html 按照Charles的提示,PC打开 chls.pro/ssl下载得到一个 ...

  9. redhat 6.6 离线安装docker

    本机环境:redhat6.6 uname -a Linux host- -.el6.x86_64 # SMP Tue Sep :: EDT x86_64 x86_64 x86_64 GNU/Linux ...

  10. centos LAMP第三部分php,mysql配置 php配置文件 配置php的error_log 配置php的open_basedir 安装php的扩展模块 phpize mysql配置第二十一节课

    centos   LAMP第三部分php,mysql配置 php配置文件   配置php的error_log  配置php的open_basedir 安装php的扩展模块 phpize  mysql配 ...