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 ...
随机推荐
- UVa 673 Parentheses Balance (stack)
题目描述 : 判断字符串是不是符合正确的表达式形式. 要点 : 考虑字符串为空的时候,用getline输入,每一次判断后如果为No则要清空栈.对称思想. 注意输入格式. 代码: #include &l ...
- 【Android N 7.1.1】 屏幕亮和灭的处理地方
frameworks/base/services/core/java/com/android/server/display/DisplayPowerController.java private vo ...
- serializeArray()与serialize()的区别
serialize()序列化表单元素为字符串,用于 Ajax 请求. serializeArray()序列化表单元素为JSON数据. <script type="text/javasc ...
- 在Linux中的.iso文件的处理方法
1,mkdir /a 2,mount MLNX_OFED_LINUX-4.4-2.0.7.0-rhel7.3-x86_64.iso /a3,cd /a4,这样就可以对文件进行操作了
- nginx 认证访问web
htpasswd -c /usr/local/nginx/passwd.db huo nginx .conf文件添加↓:
- EF的使用(DbContext对象的共用问题)
1.问题的引入 对于某一个数据库的EF操作对象,当执行某一次请求的时候,可能会多次操作数据库,也就是可能创建很多MyDbContext(继承自DbContext对象,EF上下文对象) 2.代码创建 当 ...
- html/css实现文字自动换行,超出部分出现(...)
PS:这是我在别人博客copy下来的 做前端的我们都会发现这样一个问题,当你控制文字出现多行时,而这多行是有限制的(比如超出部分隐藏不显示),而这多行文字如果全部是数字或者字母抑或是数字和字母的组合时 ...
- jQuery change事件
定义和用法 当元素的值发生改变时,会发生 change 事件. 该事件仅适用于文本域(text field),以及 textarea 和 select 元素. change() 函数触发 change ...
- Just a Hook---hdu1698(线段树---区间处理)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1698 现有n个金属(编号1---n),每个金属的材质都是铜,有m个操作,每个操作都是把编号 L 到 R ...
- 数据网格和树-EasyUI Datagrid 数据网格、EasyUI Propertygrid 属性网格、EasyUI Tree 树、EasyUI Treegrid 树形网格
EasyUI Datagrid 数据网格 扩展自 $.fn.panel.defaults.通过 $.fn.datagrid.defaults 重写默认的 defaults. 数据网格(datagrid ...