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 ...
随机推荐
- js offset
1.offsetParent offsetParent属性返回一个对象的引用,这个对象是距离调用offsetParent的元素最近的(在包含层次中最靠近的),并且是已进行过CSS定位的容器元素. 如果 ...
- oracle rank over partition by
转自:https://www.cnblogs.com/wingsless/archive/2012/02/04/2338292.html rank() over(partition)的使用 有的时 ...
- 2-sat(tarjan算法)hdu(1824)
hdu1824 Let's go home Time Limit: 10000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- Yii2 高级模板不使用Apache配置目录,将前后台入口移到根目录
刚刚入手Yii2高级模板不久,部署项目时,得部署2个应用,个人感觉很繁琐,就将前后台入口文件全部拿到项目根目录.但是一看,完了,出错了!找教程找不到,还是自己解决吧 为了以后好升级,不改变Yii2核心 ...
- RedisDesktopManager 打开报0xc000007b程序错误
RedisDesktopManager 是一个管理redis的工具,很好用,我的电脑可以安装0.8.3版的,最新版到0.9.4了,其中经典版本是0.8.8,可惜0.8.3版之后,我的电脑安装软件后,打 ...
- 洛谷 P2602 [ZJOI2010]数字计数
洛谷 第一次找规律A了一道紫题,写篇博客纪念一下. 这题很明显是数位dp,但是身为蒟蒻我不会呀,于是就像分块打表水过去. 数据范围是\(10^{12}\),我就\(10^6\)一百万一百万的打表. 于 ...
- mysql 删除表
删除表 DROP TABLE 表名;
- centos LNMP第一部分环境搭建 LAMP LNMP安装先后顺序 php安装 安装nginx 编写nginx启动脚本 懒汉模式 mv /usr/php/{p.conf.default,p.conf} php运行方式SAPI介绍 第二十三节课
centos LNMP第一部分环境搭建 LAMP安装先后顺序 LNMP安装先后顺序 php安装 安装nginx 编写nginx启动脚本 懒汉模式 mv /usr/local/php/{ ...
- OCR技术浅探:Python示例(5)
文件说明: 1. image.py——图像处理函数,主要是特征提取: 2. model_training.py——训练CNN单字识别模型(需要较高性能的服务器,最好有GPU加速,否则真是慢得要死): ...
- 朴素贝叶斯算法原理及Spark MLlib实例(Scala/Java/Python)
朴素贝叶斯 算法介绍: 朴素贝叶斯法是基于贝叶斯定理与特征条件独立假设的分类方法. 朴素贝叶斯的思想基础是这样的:对于给出的待分类项,求解在此项出现的条件下各个类别出现的概率,在没有其它可用信息下,我 ...