UVA 257 - Palinwords(字符串HASH)

UVA 257 - Palinwords

题目链接

题意:输出一个文本里面的palinword,palinword的定义为,包含两个不同的回文子串,并且要求回文子串不能互相包含

思路:对于每个单词判断一次,由于不能互相包含,对于每个位置,其实就只要找长度3和4的情况即可,这样复杂度为O(n),至于判断重复的,就用hash即可

代码:

#include <cstdio>
#include <cstring>

char str[260];
int hash[555555], save[260], sn;

bool check() {
    sn = 0;
    int n = strlen(str);
    for (int i = 1; i < n - 1; i++) {
	if (str[i - 1] == str[i + 1]) {
	    int num = (str[i - 1] - 'A' + 1) * 27 * 27 + (str[i] - 'A' + 1) * 27 + str[i + 1] - 'A' + 1;
	    if (!hash[num]) {
		hash[num] = 1;
		save[sn++] = num;
	    }
	    continue;
	}
	if (str[i] == str[i + 1] && str[i - 1] == str[i + 2]) {
	    int num = (str[i - 1] - 'A' + 1) * 27 * 27 * 27 + (str[i] - 'A' + 1) * 27 * 27 + (str[i + 1] - 'A' + 1) * 27 + str[i + 2] - 'A' + 1;
	    if (!hash[num]) {
		hash[num] = 1;
		save[sn++] = num;
	    }
	}
    }
    for (int i = 0; i < sn; i++)
	hash[save[i]] = 0;
    return sn >= 2;
}

int main() {
    while (~scanf("%s", str))
	if (check())
	    printf("%s\n", str);
    return 0;
}

UVA 257 - Palinwords(字符串HASH)

时间: 2024-07-29 16:17:30

UVA 257 - Palinwords(字符串HASH)的相关文章

UVa 257 - Palinwords

题目:判断一个字符串内是否存在两个以上互不为子串的回文串(3个字符以上). 分析:枚举.直接枚举3-4个字符的回文串即可. 说明:╮(╯▽╰)╭. #include <algorithm> #include <iostream> #include <cstdlib> #include <cstring> #include <cstdio> #include <cmath> using namespace std; bool list[

uva 10391 Compound Words (字符串-hash)

Problem E: Compound Words You are to find all the two-word compound words in a dictionary. A two-word compound word is a word in the dictionary that is theconcatenation of exactly two other words in the dictionary. Input Standard input consists of a

UVA 261 - The Window Property(字符串Hash)

UVA 261 - The Window Property 题目链接 题意:这题题意挺绕的..就是给定一个字符串长度n,扫描长度为k = [1,n],然后每次只能扫描连续k个字符的子串,要求所有扫描中,每次扫描中出现的不同字符串个数都不超过k + 1,那么这个字符串就是window property,如果不是的话,就还要找出下标最小的不符合的位置(就是n次扫描中找最小的) 思路:Hash大法好,长度才100,直接处理出hash,O(n^2)随便搞掉 代码: #include <cstdio>

UVA 11107 - Life Forms(Hash+LCP)

UVA 11107 - Life Forms 题目链接 题意:给定一个字符串,找出重复出现超过m次的字串的最大开始下标 思路:hash大法,需要点人品,然后二分答案,每次利用hash值去找出最大下标即可 代码: #include <cstdio> #include <cstring> #include <algorithm> using namespace std; typedef unsigned long long ull; const ull x = 123; c

UVa 11019 Matrix Matcher - Hash

题目传送门 快速的vjudge传送门 快速的UVa传送门 题目大意 给定两个矩阵S和T,问T在S中出现了多少次. 不会AC自动机做法. 考虑一维的字符串Hash怎么做. 对于一个长度为$l$的字符串$s$,它的Hash值$hash(s) = \sum_{i = 1}^{l}x^{l - i}s_{i}$. 对于二维的情况,我们就取两个基,$x, y$,对于一个$n\times m$的矩阵$A$的Hash值可以表示为 $hash(A) = \sum_{i = 1}^{n}\sum_{j = 1}^

hdu 1880 魔咒词典(字符串hash)

题目链接:hdu 1880 魔咒词典 题意: 给你一个10w的词典,让你输出对应的字段. 题解: map暴力存字符串肯定会卡内存,这里用BKDR字符串hash一下,用map映射一下. 1 #include<bits/stdc++.h> 2 #define F(i,a,b) for(int i=a;i<=b;++i) 3 using namespace std; 4 typedef unsigned long long ull; 5 6 const int N=1e5+7,seed=133

【二分答案+智障的字符串hash】BZOJ2946-[Poi2000]公共串(Ranklist倒一达成!!!!!)【含hash知识点】

[题目大意] 给出几个由小写字母构成的单词,求它们最长的公共子串的长度. [字符串hash的小笔记] hash[i]=(hash[i-1]*p+idx(s[i]))%mod,idx为映射值,一般a..z映射1..26: 习惯上,p取一个6到8位的素数即可,mod一般取大素数 1e9+7(1000000007)或1e9+9(1000000009). hash[i]=(hash[i-1]*p+idx(s[i]))%mod 表示第 i 个前缀的hash值,是一个hash的前缀和,那么,要求S[l…r]

hdu4821 String 字符串hash(bkdrhash)

题意:给定m,l,一个字符串str. 我们定义一个子串为"recoverable"串 当 1.长度为 M*L 2.把这个好串分成M段,每段长度为L,且每段各不相同. 求"recoverable"串的个数,串相同位置不同也算不同. 思路:预处理数组hash,hash[i]表示从i位置到尾的字符串hash值.这里数组为unsigned long long型,因为可以自动取模.然后枚举 前l个位置,每次向后翻滚(将前一个长度为l的段hash值从map中去除,将翻滚后新出现

字符串hash + 二分答案 - 求最长公共子串 --- poj 2774

Long Long Message Problem's Link:http://poj.org/problem?id=2774 Mean: 求两个字符串的最长公共子串的长度. analyse: 前面在学习后缀数组的时候已经做过一遍了,但是现在主攻字符串hash,再用字符串hash写一遍. 这题的思路是这样的: 1)取较短的串的长度作为high,然后二分答案(每次判断长度为mid=(low+high)>>1是否存在,如果存在就增加下界:不存在就缩小上界): 2)主要是对答案的判断(judge函数