思路:给你字符串,如果他包含至少两个长度大于等于3的回文,并且这些回文不能嵌套(例如aaa嵌套在aaaa,waw嵌套在awawa),如果这个字符串这么牛逼的话,就输出他。

思路:拿到字符串先正序hash和逆序hash,用来判断回文串。这里其实只要判断长度为3和4的回文就行,因为3,4大的可以嵌套在比他大的里面。一开始我还在想怎么区分aaa和aaaa,弄了个很复杂的东西,但是我发现其实只要储存回文串的半径的hash就行了!这样长度3和4也能比了。

代码:

#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];
ull ha[][maxn],bin[maxn];
void init(){
bin[] = ;
for(int i = ; i <= ; i++)
bin[i] = bin[i - ] * seed;
}
void HASH(int len){
ha[][] = ;
for(int i = ; i <= len; i++) //顺序
ha[][i] = ha[][i - ] * seed + s[i];
ha[][] = ;
for(int i = len,j = ; i >= ; i--, j++) //逆序
ha[][j] = ha[][j - ] * seed + s[i];
}
ull getsub(int l,int r,int id){
return ha[id][r] - ha[id][l - ] * bin[r - l + ];
}
int main(){
init();
while(scanf("%s", s + ) != EOF){
int len = strlen(s + );
HASH(len);
int flag = -;
ull is;
for(int i = ; i <= len - ; i++){
ull suf = getsub(i, i + ,); //顺序
ull pre = getsub(len - (i + ) + , len - (i + ) + , ); //逆序
if(suf == pre){
if(flag == -){
is = suf;
flag = ;
}
else if(suf != is){
flag = -;
break;
}
}
}
if(flag == -){
printf("%s\n", s + );
continue;
}
for(int i = ; i<= len - ; i++){
ull suf = getsub(i, i + ,); //顺序
ull pre = getsub(len - (i + ) + , len - (i + ) + , ); //逆序
if(suf == pre){
if(flag == -){
is = suf;
flag = ;
}
else if(suf != is){
flag = -;
break;
}
}
}
if(flag == -){
printf("%s\n", s + );
continue;
}
}
return ;
}

UVA 257 Palinwords(hash)题解的更多相关文章

  1. UVA 257 - Palinwords(弦HASH)

    UVA 257 - Palinwords 题目链接 题意:输出一个文本里面的palinword,palinword的定义为.包括两个不同的回文子串,而且要求回文子串不能互相包括 思路:对于每一个单词推 ...

  2. uva 6959 Judging hash

    Judging Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/problem/viewProb ...

  3. UVA - 11604 General Sultan 题解

    题目大意: 有若干模式串,将某些模式串拼接起来(一个可以使用多次)形成一个长模式串,判断能否有两种或更多种不同的拼法拼成相同的模式串. 思路: 神奇的构图,暴力的求解. 可以发现,若有不同的拼法,则一 ...

  4. Codeforces Round #257 (Div. 2) 题解

    Problem A A. Jzzhu and Children time limit per test 1 second memory limit per test 256 megabytes inp ...

  5. [UVA] 704 Colour Hash

    所谓"周界搜索",练习搜索的好题,双向宽搜/迭代加深均可,还有很多细节有待完善,判重有比set更优的结构,宽搜还没写,先存一下. //Writer:GhostCai &&a ...

  6. UVa 202 Repeating Decimals 题解

    The decimal expansion of the fraction 1/33 is 0.03, where the 03 is used to indicate that the cycle ...

  7. UVA 10924 Prime Words 题解

    Prime Words A prime number is a number that has only two divisors: itself and the number one. Exampl ...

  8. UVA 10852 Less Prime 题解

    Less Prime Let n be an integer, 100 n 10000, nd the prime number x, x n, so that n

  9. UVA 10537 The Toll! Revisited uva1027 Toll(最短路+数学坑)

    前者之所以叫加强版,就是把uva1027改编了,附加上打印路径罢了. 03年的final题哦!!虽然是水题,但不是我这个只会做图论题的跛子能轻易尝试的——因为有个数学坑. 题意:运送x个货物从a-&g ...

随机推荐

  1. jQuery数据缓存

    jQuery引入数据缓存机制的原因: 1.储存更DOM节点相关的数据.事件.动画等信息 2.用一种低耦合的方式让DOM节点和数据联系起来 实现原理: 1.jQuery内部创建cache对象 2.为需要 ...

  2. session.setAttribute和session.getAttribute

    网上搜了些资料 ----------------------------------------------------------------------------- B/S架构中,客户端与服务器 ...

  3. Intel产品AMT本地及远程提权漏洞(CVE-2017-5689)复现 【转载自freebuf.com】

    零.绪论: 1.鸣谢freebuf的文章,主要是学习这个漏洞,文章地址: Intel产品AMT本地及远程提权漏洞(CVE-2017-5689)复现 2.在shadon上找了多个该漏洞尝试复现失败(评论 ...

  4. PHP 允许Ajax跨域访问 (Access-Control-Allow-Origin)

    Ajax访问php,报错 php顶部加上即可: header("Access-Control-Allow-Origin: *");

  5. Oracle自动备份脚本的实现

    问题描述: Oracle自动备份脚本的实现. 错误提示1: Message file RMAN.msb not found Verify that Oracle_HOME is set properl ...

  6. Oracle涂抹oracle学习笔记第8章RMAN说,我能备份

    本次测试服务器为172.16.25.33 使用rman连接本地数据库 rman target / 在rman中执行启动与关闭的命令与sqlplus相同 在rman中执行sql语句 sql ‘需要执行的 ...

  7. mongoexport

    导数据 数据同步 mongodb无自增id 数据断点 mongoexport — MongoDB Manual https://docs.mongodb.com/manual/reference/pr ...

  8. 最近遇到的bug

    1. 地图周边快查,按钮点击没反应 子控件超出了父控件 2.图片显示灰色背景,一直去不掉    设置图片背景图片clear cloro  3. 显示隐藏导航栏  下面两个方法效果不同     self ...

  9. Strategic Game--hdu1054(最小覆盖点)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1054 求最小覆盖点,也就是求最大匹配,要用邻接表写,不然会TLE:当然也可以用HK算法: #inclu ...

  10. 【剑指Offer】俯视50题之1-10题

    面试题1赋值运算符函数  面试题2 实现Singleton模式  面试题3 二维数组中的查找   面试题4 替换空格   面试题5 从头到尾打印链表   面试题6 重建二叉树   面试题7 用两个栈实 ...