2020 CCPC Wannafly Winter Camp Day2 K 破忒头的匿名信(AC自动机)

在这里插入图片描述

题意:

给定 N N 个单词和字符串 T T i i 个单词是一个代价为 p i p_i 的字符串 S i S_i 求用这些单词(可重复使用)组成 T T 的最小代价,若无法组成 T T 则输出 1 -1 .

保证所有所有字符串都仅包含小写英文字母,且 i = 1 N S i 5 × 1 0 5 \sum\limits_{i=1}^N\left|S_i\right|\leq5\times10^5

所有单词长度之和不大,不同长度单词的数量是根号级别的。
可用 A C AC 自动机处理出 TT 的每个位置的有效前继转移位置。
则问题等价于在 D A G DAG 上求最短路,直接 D P DP 求解即可。

AC代码:

#include <cstdio>
#include <vector>
#include <queue>
#include <cstring>
#include <cmath>
#include <map>
#include <set>
#include <string>
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <stack>
#include <queue>
using namespace std;
#define sd(n) scanf("%d", &n)
#define sdd(n, m) scanf("%d%d", &n, &m)
#define sddd(n, m, k) scanf("%d%d%d", &n, &m, &k)
#define pd(n) printf("%d\n", n)
#define pc(n) printf("%c", n)
#define pdd(n, m) printf("%d %d\n", n, m)
#define pld(n) printf("%lld\n", n)
#define pldd(n, m) printf("%lld %lld\n", n, m)
#define sld(n) scanf("%lld", &n)
#define sldd(n, m) scanf("%lld%lld", &n, &m)
#define slddd(n, m, k) scanf("%lld%lld%lld", &n, &m, &k)
#define sf(n) scanf("%lf", &n)
#define sc(n) scanf("%c", &n)
#define sff(n, m) scanf("%lf%lf", &n, &m)
#define sfff(n, m, k) scanf("%lf%lf%lf", &n, &m, &k)
#define ss(str) scanf("%s", str)
#define rep(i, a, n) for (int i = a; i <= n; i++)
#define per(i, a, n) for (int i = n; i >= a; i--)
#define mem(a, n) memset(a, n, sizeof(a))
#define debug(x) cout << #x << ": " << x << endl
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define mod(x) ((x) % MOD)
#define gcd(a, b) __gcd(a, b)
#define lowbit(x) (x & -x)
typedef pair<int, int> PII;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const int MOD = 1e9 + 7;
const double eps = 1e-9;
const ll INF = 0x3f3f3f3f3f3f3f3fll;
const int inf = 0x3f3f3f3f;
inline int read()
{
    int ret = 0, sgn = 1;
    char ch = getchar();
    while (ch < '0' || ch > '9')
    {
        if (ch == '-')
            sgn = -1;
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9')
    {
        ret = ret * 10 + ch - '0';
        ch = getchar();
    }
    return ret * sgn;
}
inline void Out(int a)
{
    if (a > 9)
        Out(a / 10);
    putchar(a % 10 + '0');
}

ll gcd(ll a, ll b)
{
    return b == 0 ? a : gcd(b, a % b);
}

ll lcm(ll a, ll b)
{
    return a * b / gcd(a, b);
}
///快速幂m^k%mod
ll qpow(ll a, ll b, ll mod)
{
    if (a >= mod)
        a = a % mod + mod;
    ll ans = 1;
    while (b)
    {
        if (b & 1)
        {
            ans = ans * a;
            if (ans >= mod)
                ans = ans % mod + mod;
        }
        a *= a;
        if (a >= mod)
            a = a % mod + mod;
        b >>= 1;
    }
    return ans;
}

// 快速幂求逆元
int Fermat(int a, int p) //费马求a关于b的逆元
{
    return qpow(a, p - 2, p);
}

///扩展欧几里得
int exgcd(int a, int b, int &x, int &y)
{
    if (b == 0)
    {
        x = 1;
        y = 0;
        return a;
    }
    int g = exgcd(b, a % b, x, y);
    int t = x;
    x = y;
    y = t - a / b * y;
    return g;
}

///使用ecgcd求a的逆元x
int mod_reverse(int a, int p)
{
    int d, x, y;
    d = exgcd(a, p, x, y);
    if (d == 1)
        return (x % p + p) % p;
    else
        return -1;
}

///中国剩余定理模板0
ll china(int a[], int b[], int n) //a[]为除数,b[]为余数
{
    int M = 1, y, x = 0;
    for (int i = 0; i < n; ++i) //算出它们累乘的结果
        M *= a[i];
    for (int i = 0; i < n; ++i)
    {
        int w = M / a[i];
        int tx = 0;
        int t = exgcd(w, a[i], tx, y); //计算逆元
        x = (x + w * (b[i] / t) * x) % M;
    }
    return (x + M) % M;
}

const int N = 1e5 + 5;
int n, p;
int edg = 1, head[N], to[N], nxt[N];
int siz[N], son[N];
set<int> s[N];
ll ans[N];

void add_edge(int u, int v)
{
    to[++edg] = v, nxt[edg] = head[u], head[u] = edg;
}


void insert(int id, int v, int u)
{
    auto y = s[id].lower_bound(v);
    if (y == s[id].begin())
        ans[u] += (v - *y) * 1ll * (v - *y);
    else if (y == s[id].end())
        y--, ans[u] += (*y - v) * 1ll * (*y - v);
    else
    {
        auto x = y--;
        ans[u] -= (*x - *y) * 1ll * (*x - *y);
        ans[u] += (*x - v) * 1ll * (*x - v) + (v - *y) * 1ll * (v - *y);
    }
    s[id].insert(v);
}

void get_son(int u)
{
    siz[u] = 1;
    for (int i = head[u]; i; i = nxt[i])
    {
        int v = to[i];
        get_son(v), siz[u] += siz[v];
        son[u] = siz[v] > siz[son[u]] ? v : son[u];
    }
}

void get_ans(int u)
{
    if (!son[u])
        return s[u].insert(u), void();
    get_ans(son[u]), ans[u] = ans[son[u]];
    s[u].swap(s[son[u]]);
    insert(u, u, u);
    for (int i = head[u]; i; i = nxt[i])
    {
        int v = to[i];
        if (v == son[u])
            continue;
        get_ans(v);
        for (int x : s[v])
            insert(u, x, u);
        s[v].clear();
    }
}

int main()
{
    sd(n);
    rep(i, 2, n)
    {
        sd(p);
        add_edge(p, i);
    }
    get_son(1);
    get_ans(1);
    rep(i, 1, n)
        pld(ans[i]);
    return 0;
}

发布了679 篇原创文章 · 获赞 410 · 访问量 21万+

猜你喜欢

转载自blog.csdn.net/qq_43627087/article/details/104446214