UVA 257 Palinwords(hash)题解
思路:给你字符串,如果他包含至少两个长度大于等于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)题解的更多相关文章
- UVA 257 - Palinwords(弦HASH)
UVA 257 - Palinwords 题目链接 题意:输出一个文本里面的palinword,palinword的定义为.包括两个不同的回文子串,而且要求回文子串不能互相包括 思路:对于每一个单词推 ...
- uva 6959 Judging hash
Judging Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/problem/viewProb ...
- UVA - 11604 General Sultan 题解
题目大意: 有若干模式串,将某些模式串拼接起来(一个可以使用多次)形成一个长模式串,判断能否有两种或更多种不同的拼法拼成相同的模式串. 思路: 神奇的构图,暴力的求解. 可以发现,若有不同的拼法,则一 ...
- Codeforces Round #257 (Div. 2) 题解
Problem A A. Jzzhu and Children time limit per test 1 second memory limit per test 256 megabytes inp ...
- [UVA] 704 Colour Hash
所谓"周界搜索",练习搜索的好题,双向宽搜/迭代加深均可,还有很多细节有待完善,判重有比set更优的结构,宽搜还没写,先存一下. //Writer:GhostCai &&a ...
- 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 ...
- UVA 10924 Prime Words 题解
Prime Words A prime number is a number that has only two divisors: itself and the number one. Exampl ...
- UVA 10852 Less Prime 题解
Less Prime Let n be an integer, 100 n 10000, nd the prime number x, x n, so that n
- UVA 10537 The Toll! Revisited uva1027 Toll(最短路+数学坑)
前者之所以叫加强版,就是把uva1027改编了,附加上打印路径罢了. 03年的final题哦!!虽然是水题,但不是我这个只会做图论题的跛子能轻易尝试的——因为有个数学坑. 题意:运送x个货物从a-&g ...
随机推荐
- CodeForces - 512B Fox And Jumping[map优化dp]
B. Fox And Jumping time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- POJ 3461 Oulipo[附KMP算法详细流程讲解]
E - Oulipo Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit ...
- 高中生的IT之路-1.3那一幕
上一篇讲到,当时我认为自己的命运就是小时候上学,长大后外出打工,所以高中毕业后就来到了天津,到爸爸的店铺打工. 我爸的店铺就在天津大学校园里,幸运的是,我人生的转折点也就在此. 刚到店里那段时间,每天 ...
- SignalR实现在线聊天室功能
一.在线聊天室 1.新建解决方案 SignalROnlineChatDemo 2.新建MVC项目 SignalROnlineChatDemo.Web (无身份验证) 3.安装SignalR PM> ...
- 【BZOJ4774/4006】修路/[JLOI2015]管道连接 斯坦纳树
[BZOJ4774]修路 Description 村子间的小路年久失修,为了保障村子之间的往来,法珞决定带领大家修路.对于边带权的无向图 G = (V, E),请选择一些边,使得1 <= i & ...
- Linux系统修改编码(转)
Windows的默认编码为GBK,Linux的默认编码为UTF-8.在Windows下编辑的中文,在Linux下显示为乱码.为了解决此问题,修改Linux的默认编码为GBK.方法如下: 方法1: vi ...
- 由于dns服务为启动导致的GI集群启动故障
1.物业由于突然断电导致grid集群重新启动后rac数据库无法正常启动,对集群进行检查,结果如下,发现其中有4个数据库状态为instance shutdown.[root@node1 ~]# su - ...
- Rancher OS
Rancher OS 是生产规模中运行 Docker 最小,最简单的方式.RancherOS 的所有东西都作为 Docker 管理的容器.这些系统服务包括 udev 和 rsyslog.Rancher ...
- scrapy爬虫系列之五--CrawlSpider的使用
功能点:CrawlSpider的基本使用 爬取网站:保监会 主要代码: cf.py # -*- coding: utf-8 -*- import scrapy from scrapy.linkextr ...
- nodejs中使用linq
官网地址 https://github.com/mihaifm/linq 安装 npm install linq 导入 var Enumerable = require(‘linq‘); 例子 1 总 ...