zoukankan      html  css  js  c++  java
  • SPOJ

    题目连接:

    https://vjudge.net/problem/SPOJ-SUBLEX

    Description

    Little Daniel loves to play with strings! He always finds different ways to have fun with strings! Knowing that, his friend Kinan decided to test his skills so he gave him a string S and asked him Q questions of the form:

    If all distinct substrings of string S were sorted lexicographically, which one will be the K-th smallest?

    After knowing the huge number of questions Kinan will ask, Daniel figured out that he can't do this alone. Daniel, of course, knows your exceptional programming skills, so he asked you to write him a program which given S will answer Kinan's questions.

    Input

    In the first line there is Kinan's string S (with length no more than 90000 characters). It contains only small letters of English alphabet. The second line contains a single integer Q (Q <= 500) , the number of questions Daniel will be asked. In the next Q lines a single integer K is given (0 < K < 2^31).

    Output

    Output consists of Q lines, the i-th contains a string which is the answer to the i-th asked question.

    Sample Input

    aaa
    2
    2
    3

    Sample Output

    aa
    aaa

    Hint

    题意

    求一个字符串的所有字串中字典序第K的子串,相同子串只计算一次

    题解:

    先对后缀自动机dp,f[i]表示从节点i出发的不相同子串个数 $ f[i] = 1 + sum f[j]$
    dp的过程中注意剪枝,如果f[i]已经计算过就不用往下dfs了,不然如果建出来的自动机是个菊花图,复杂度就会爆炸
    最后在后缀自动机上dfs求出第k小子串就好了

    代码

    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    
    using namespace std;
    const int mx = 1e6+5;
    
    struct SAM_automaton {
        int Next[mx][26], len[mx], fa[mx];
        int last, tot;
        int newnode() {
            tot++;
            for (int i = 0; i < 26; i++) Next[tot][i] = 0;
            return tot;
        }
    
        void init() {
            tot = 0;
            last = newnode();
        }
    
        void add(int c) {
            int p = last;
            int np = last = newnode();
            len[np] = len[p] + 1;
            while (p && !Next[p][c]) {
                Next[p][c] = np;
                p = fa[p];
            }
            if (!p) fa[np] = 1;
            else {
                int q = Next[p][c];
                if (len[q] == len[p] + 1) fa[np] = q;
                else {
                    int nq = newnode();
                    len[nq] = len[p] + 1;
                    fa[nq] = fa[q];
                    for (int i = 0; i < 26; i++) Next[nq][i] = Next[q][i];
                    fa[q] = fa[np] = nq;
                    while (p && Next[p][c] == q) {
                        Next[p][c] = nq;
                        p = fa[p];
                    }
                }
            }
        }
    }SAM;
    
    char str[mx];
    int r[mx], b[mx], id[mx], f[mx], ans[mx];
    
    void dfs(int deep, int now, int tot) {
        if (--tot == 0) {
            for (int i = 1; i < deep; i++) putchar(ans[i]+'a');
            putchar('
    ');
            return;
        }
        for (int i = 0; i < 26; i++) {
            int np = SAM.Next[now][i];
            if (np) {   
                if (tot > f[np]) tot -= f[np];
                else {            
                    ans[deep] = i;
                    return dfs(deep+1, np, tot);
                }
            }
        }
        
    }
    
    int build(int u) {
        if (f[u]) return f[u];
        f[u] = 1;
        for (int i = 0; i < 26; i++) {
            if (SAM.Next[u][i])
                f[u] += build(SAM.Next[u][i]);
        }
        return f[u];
    }
    
    int main() {
        SAM.init();
        scanf("%s", str);
        int len = strlen(str);
        for (int i = 0; i < len; i++) SAM.add(str[i] - 'a');
        build(1);
    
        int q;
        scanf("%d", &q);
        while (q--) {
            int tot;
            scanf("%d", &tot);
            dfs(1, 1, tot+1);
        }
        return 0;
    }
    
  • 相关阅读:
    B
    Labyrinth 树的直径加DFS
    Speech to Text for iOS
    苹果开发者:Siri未开放API 有些让人失望
    ios6.0 siri语音识别
    Sample example for Speech to Text in iOS
    iOS升级经验分享
    苹果放宽了 iOS 5.0 对应用本地存储的限制
    iOS5可能会删除本地文件储存
    iOS 5 does not allow to store downloaded data in Documents directory? ios5.0及以后的版本对于下载的文件存储路径有了改变
  • 原文地址:https://www.cnblogs.com/bpdwn-cnblogs/p/11230620.html
Copyright © 2011-2022 走看看