其他分享
首页 > 其他分享> > TJOI 2015 弦论

TJOI 2015 弦论

作者:互联网

子串,容易想到 \(SAM\)

首先类似模板题一样的记录 \(num(v)\) 表示有多少子串经过 \(v\)

\(sum(v)\) 表示状态 \(v\) 在字符串中的出现次数

如果 \(T = 1\) 显然 \(sum(v)=1\)

否则,发现 \(sum(v)\) 实际就是其后继节点的 \(\sum_{}num(y)\)

然后 \(O(n)\) 即可扫除答案

#include <map>
#include <set>
#include <ctime>
#include <queue>
#include <stack>
#include <cmath>
#include <vector>
#include <bitset>
#include <cstdio>
#include <cctype>
#include <string>
#include <numeric>
#include <cstring>
#include <cassert>
#include <climits>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std ;
//#define int long long
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define loop(s, v, it) for (s::iterator it = v.begin(); it != v.end(); it++)
#define cont(i, x) for (int i = head[x]; i; i = e[i].nxt)
#define clr(a) memset(a, 0, sizeof(a))
#define ass(a, sum) memset(a, sum, sizeof(a))
#define lowbit(x) (x & -x)
#define all(x) x.begin(), x.end()
#define ub upper_bound
#define lb lower_bound
#define pq priority_queue
#define mp make_pair
#define pb push_back
#define pof pop_front
#define pob pop_back
#define fi first
#define se second
#define iv inline void
#define enter cout << endl
#define siz(x) ((int)x.size())
#define file(x) freopen(x".in", "r", stdin),freopen(x".out", "w", stdout)
typedef long long ll ;
typedef unsigned long long ull ;
typedef pair <int, int> pii ;
typedef vector <int> vi ;
typedef vector <pii> vii ;
typedef queue <int> qi ;
typedef queue <pii> qii ;
typedef set <int> si ;
typedef map <int, int> mii ;
typedef map <string, int> msi ;
const int N = 1000010 ;
const int INF = 0x3f3f3f3f ;
const int iinf = 1 << 30 ;
const ll linf = 2e18 ;
const int MOD = 1000000007 ;
const double eps = 1e-7 ;
void douout(double x){ printf("%lf\n", x + 0.0000000001) ; }
template <class T> void print(T a) { cout << a << endl ; exit(0) ; }
template <class T> void chmin(T &a, T b) { if (a > b) a = b ; }
template <class T> void chmax(T &a, T b) { if (a < b) a = b ; }
template <class T> void upd(T &a, T b) { (a += b) %= MOD ; }
template <class T> void mul(T &a, T b) { a = (ll) a * b % MOD ; }

int n, t, k, p, np, q, nq, last = 1, cnt = 1 ;
int sum[N], num[N], c[N], a[N] ;
char s[N] ;

struct node {
    int to[26], fa, len ;
} po[N] ;

void add(int c) {
    p = last ;
    last = np = ++cnt ;
    po[np].len = po[p].len + 1 ; num[np] = 1 ;
    for (; p && !po[p].to[c]; p = po[p].fa) po[p].to[c] = np ;
    if (!p) po[np].fa = 1 ;
    else {
        q = po[p].to[c] ;
        if (po[q].len == po[p].len + 1) po[np].fa = q ;
        else {
            nq = ++cnt ;
            po[nq] = po[q] ;
            po[nq].len = po[p].len + 1 ;
            po[np].fa = po[q].fa = nq ;
            for (; p && po[p].to[c] == q; p = po[p].fa) po[p].to[c] = nq ;
        }
    }
}

void work() {
    rep(i, 1, cnt) c[po[i].len]++ ;
    rep(i, 1, cnt) c[i] += c[i - 1] ;
    rep(i, 1, cnt) a[c[po[i].len]--] = i ;
    per(i, cnt, 1) {
        if (t) num[po[a[i]].fa] += num[a[i]] ;
        else num[a[i]] = 1 ;
    }
    num[1] = 0 ;
    per(i, cnt, 1) {
        sum[a[i]] = num[a[i]] ;
        rep(j, 0, 25)
        if (po[a[i]].to[j])
        sum[a[i]] += sum[po[a[i]].to[j]] ;
    }
}

void solve() {
    if (k > sum[1]) {
        puts("-1") ;
        return ;
    }
    int now = 1 ;
    k -= num[now] ;
    while (k > 0) {
        int p = 0 ;
        while (k > sum[po[now].to[p]]) {
            k -= sum[po[now].to[p]] ;
            p++ ;
        }
        now = po[now].to[p] ;
        putchar('a' + p) ;
        k -= num[now] ;
    }
}

signed main(){
//  file("test") ;
    scanf("%s%d%d", s + 1, &t, &k) ;
    n = strlen(s + 1) ;
    rep(i, 1, n) add(s[i] - 'a') ;
    work() ;
    solve() ;
    return 0 ;
}

/*
写代码时请注意:
    1.ll?数组大小,边界?数据范围?
    2.精度?
    3.特判?
    4.至少做一些
思考提醒:
    1.最大值最小->二分?
    2.可以贪心么?不行dp可以么
    3.可以优化么
    4.维护区间用什么数据结构?
    5.统计方案是用dp?模了么?
    6.逆向思维?
*/


标签:int,TJOI,po,num,sum,2015,include,弦论,define
来源: https://www.cnblogs.com/harryhqg/p/10725073.html