uva 12206 - Stammering Aliens(哈希)

题目链接:uva 12206 - Stammering Aliens

题目大意:给出一个字符串,找出至少出现m次的最长子串。

解题思路:哈希算法,将每个后缀数组建立一个哈希值,每次二分长度判断,每次判断时将哈希值排序,计数即可。

#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

typedef unsigned long long ll;
const int maxn = 40005;
const int x = 123;

int N, M, pos, Rank[maxn];
char str[maxn];
ll h[maxn], xp[maxn], Hash[maxn];

void init () {
    scanf("%s", str);
    N = strlen(str);

    h[N] = 0;
    for (int i = N - 1; i >= 0; i--)
        h[i] = h[i+1] * x + str[i] - ‘a‘;
    xp[0] = 1;
    for (int i = 1; i <= N; i++)
        xp[i] = xp[i-1] * x;
}

bool cmp (const int& a, const int& b) {
    return Hash[a] < Hash[b] || (Hash[a] == Hash[b] && a < b);
}

bool judge (int l) {
    int c = 0, n = N - l + 1;
    pos = -1;

    for (int i = 0; i < n; i++) {
        Rank[i] = i;
        Hash[i] = h[i] - h[i+l] * xp[l];
    }

    sort (Rank, Rank + n, cmp);
    for (int i = 0; i < n; i++) {
        if (i == 0 || Hash[Rank[i]] != Hash[Rank[i-1]]) c = 0;
        if (++c >= M)
            pos = max(pos, Rank[i]);
    }
    return pos >= 0;
}

void bsearch () {
    if (!judge(1)) {
        printf("none\n");
        return;
    }

    int l = 1, r = N + 1;
    while (r - l > 1) {
        int mid = (r + l) / 2;
        if (judge(mid))
            l = mid;
        else
            r = mid;
    }
    judge(l);
    printf("%d %d\n", l, pos);
}

int main () {
    while (scanf("%d", &M) == 1 && M) {
        init();
        bsearch();
    }
    return 0;
}
时间: 2024-08-06 07:51:32

uva 12206 - Stammering Aliens(哈希)的相关文章

UVA 12206 - Stammering Aliens(后缀数组)

UVA 12206 - Stammering Aliens 题目链接 题意:给定一个序列,求出出现次数大于m,长度最长的子串的最大下标 思路:后缀数组,搞出height数组后,利用二分去查找即可 这题之前还写过hash的写法也能过,不过写后缀数组的时候,犯了一个傻逼错误,把none输出成node还一直找不到...这是刷题来第二次碰到这种逗比错误了,还是得注意.. 代码: #include <cstdio> #include <cstring> #include <algori

UVA - 12206 Stammering Aliens (hash)

这题其实很容易想到2分长度,关键是2分后,怎么判断出现最多次的串是否>=m次了.这里需要用到hash来处理. 对于一个字符串   H[i] =H[i+1]*131+s[i]  ;其中 H[n]=0:那么对于一个长度为L的串 ,hanh[i,l]=H[i]-H[i+L]*x^L ; 这样记录每个字符串的hash值,然后再处理起来就比较简单了. VIEW CODE #include<cstdio> #include<algorithm> #include<iostream&

Uva 12206 Stammering Aliens

题目描述 输入输出格式 输入格式: 输出格式: 输入输出样例 输入样例#1: 3 baaaababababbababbab 11 baaaababababbababbab 3 cccccc 0 输出样例#1: 5 12 none 4 2 这个题后缀数组或者二分+hash好像都能做,,,但是我只是练一下后缀自动机而已hhhhh这个题我们求的显然就是right集合大小>=m的max{}的最大值,至于求最右开端的话,我们只需要记录一下right集合的最右点是哪个就行了,然后用这个减去答案长度就是最右开

uva 1390 - Interconnect(期望+哈希+记忆化)

题目连接:uva 1390 - Interconnect 题目大意:给出n表示有n个点,m表示有m条边,现在任选两点建立一条边,直到整个图联通,问说还需建立边数的期望,建过边的两点仍可以建边. 解题思路:哈希的方法很是巧妙,将各个联通分量中节点的个数c[i]转换成一个30进制的数(因为节点个数最多为30),因为结果很大,所以对1e5+7取模.获得的哈希值作为插入和搜索的起点. #include <cstdio> #include <cstring> #include <alg

POJ 3882 Stammering Aliens 后缀数组height应用

题目来源:POJ 3882 Stammering Aliens 题意:给你m一个一个字符串 求至少出现m次的最长字符串 可以在字符串中重叠出现 思路:二分长度l 然后从height数组中找长度大于等于l的前缀 #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int maxn = 40010; char s[maxn]; int sa[maxn]; i

POJ 3026 Borg Maze &amp; UVA 10307 Killing Aliens in Borg Maze(BFS,最小生成树)

http://poj.org/problem?id=3026 http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1248 Borg Maze Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8498   Accepted: 2862 Description The Bor

LA 4513 Stammering Aliens 字符串hash

字符串hash模板, 本题是求,给定字符串s中至少出现m次的最长字符串长度,及此时起始位置的最大值 LA 4513  Stammering Aliens 白书p225 //#pragma warning (disable: 4786) //#pragma comment (linker, "/STACK:16777216") //HEAD #include <cstdio> #include <ctime> #include <cstdlib> #i

UVa 12206 (字符串哈希) Stammering Aliens

体验了一把字符串Hash的做法,感觉Hash这种人品算法好神奇. 也许这道题的正解是后缀数组,但Hash做法的优势就是编码复杂度大大降低. 1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 using namespace std; 5 6 const int maxn = 40000 + 10; 7 const int x = 123; 8 int n, m, pos; 9 unsigne

UVA 11019 字符矩阵哈希

思路:以前没做过字符矩阵的哈希,所以这题是看别人博客写的. #pragma comment(linker, "/STACK:1024000000,1024000000") #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<map> #include<queue> #include<set>