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 ...
 
随机推荐
- 关于Activity的getReferrer():如何在Activity中获取调用者?
			
http://blog.csdn.net/u013553529/article/details/53856800 关于Activity的getReferrer()之一:如何在Activity中获取调用 ...
 - Python 中的线程-进程2
			
原文:https://www.cnblogs.com/i-honey/p/7823587.html Python中实现多线程需要使用到 threading 库,其中每一个 Thread类 的实例控制一 ...
 - java中生成流水号的一个例子(使用BerkeleyDB)
			
package com.jiaoyiping.berkeleydb; import com.sleepycat.je.*; import com.sleepycat.utilint.StringUti ...
 - iOS 如何在视图中添加一个用xib创建的view
			
NSArray *nib = [[NSBundle mainBundle]loadNibNamed:[pages objectAtIndex:] owner:self options:nil]; // ...
 - WCF(四)  绑定
			
绑定 是一个制定好的通道栈,包含了协议通道,传输通道和编码器.从功能上来看,一个绑定集成了通信模式.可靠性.安全性.事务传播和互操作性 绑定方式分两种:代码中和配置文件中绑定 1: 2: 3.配置ap ...
 - JavaScript获取地址栏的参数!
			
第一种方式:手动解析 “location”对象 console.log(window.location); 使用这个对象做跳转: window.location.href=""; ...
 - 沈阳网络赛F-Fantastic Graph【贪心】or【网络流】
			
"Oh, There is a bipartite graph.""Make it Fantastic." X wants to check whether a ...
 - 转!!java 堆栈内存 对象实例等查看
			
https://blog.csdn.net/fenglibing/article/details/6411999
 - 因为smb和nfs挂掉导致客户端开机启动不了
			
因为smb和nfs挂掉导致客户端开机启动不了 虚拟机里的Linux启动不起来 决定开机 按住esc键,看一下到底启动到哪一步出错 从图中可以看出是挂载文件系统的时候出错,文件系统是CIFS, 是sam ...
 - PHP debug_backtrace() 函数打印调用处的调试信息
			
http://php.net/manual/zh/function.debug-backtrace.php debug_backtrace (PHP 4 >= 4.3.0, PHP 5, PHP ...