设为首页 加入收藏

TOP

BZOJ4598: [Sdoi2016]模式字符串(点分治 hash)
2018-12-05 22:08:46 】 浏览:14
Tags:BZOJ4598: Sdoi2016 模式 字符串 分治 hash

题意

题目链接

Sol

直接考虑点分治+hash匹配

\(up[i]\)表示\(dep \% M = i\)的从下往上恰好与前\(i\)位匹配的个数

\(down\)表示\(dep \% M = i\)的从上往下恰好与后\(i\)位匹配的个数

暴力转移即可

复杂度:\(O(nlog^2n)??\)

代码写起来有一车边界

#include<bits/stdc++.h>
#define ull unsigned long long 
#define LL long long 
#define int long long 
#define siz(v) ((int)v.size())
using namespace std;
const int MAXN = 1e6 + 10, INF = 1e10 + 10;
inline int read() {
    char c = getchar(); int x = 0, f = 1;
    while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
    while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
    return x * f;
}
int N, M, Root, siz[MAXN], mx[MAXN], Siz, dep[MAXN], up[MAXN], down[MAXN], su[MAXN], sd[MAXN];
LL ans;
bool det[MAXN];
char a[MAXN], b[MAXN];
vector<int> v[MAXN];
ull hs[MAXN], hp[MAXN], po[MAXN], base = 1331;
map<ull, bool> mp;
void FindRoot(int x, int fa) {
    siz[x] = 1; mx[x] = 0;
    for(int i = 0; i < siz(v[x]); i++) {
        int to = v[x][i];
        if(to == fa || det[to]) continue;
        FindRoot(to, x);
        siz[x] += siz[to]; mx[x] = max(mx[x], siz[to]);
    }
    mx[x] = max(mx[x], Siz - siz[x]);
    if(mx[x] < mx[Root]) Root = x;
}
int dfs(int x, int fa, ull now) {
    siz[x] = 1;
    dep[x] = dep[fa] + 1;
    now = now * base + a[x];
    if(hp[dep[x]] == now) up[(dep[x] - 1) % M + 1]++, ans += sd[M - (dep[x] - 1) % M];
    if(hs[dep[x]] == now) down[(dep[x] - 1) % M + 1]++, ans += su[M - (dep[x] - 1) % M];
   // printf("%d %d\n", x, ans);
    int td =1;
    for(int i = 0; i < siz(v[x]); i++) {
        int to = v[x][i];
        if(to == fa || det[to]) continue;
        td = max(td, dfs(to, x, now) + 1); 
        siz[x] += siz[to];
    }
    return td;
}
void work(int x) {
    int tk = 0, tmp = 0;
    det[x] = 1; dep[x] = 1; su[1] = sd[1] = 1;//tag;
    for(int i = 0; i < siz(v[x]); i++) {   
        int to = v[x][i];   
        if(det[to]) continue;
        tk = min(M, dfs(to, x, a[x]) + 1), tmp = max(tmp, tk);
        for(int j = 1; j <= tk; j++) su[j] += up[j], sd[j] += down[j], up[j] = down[j] = 0;
    }
    for(int i = 1; i <= tmp; i++) su[i] = sd[i] = 0;
    for(int i = 0; i < siz(v[x]); i++) {
        int to = v[x][i];
        if(to == x || det[to]) continue;
        Siz = siz[to]; Root = 0; FindRoot(to, x); 
        work(Root);
    }
}
void init() {
    for(int i = 1; i <= N; i++) v[i].clear();
    memset(det, 0, sizeof(det));
    memset(siz, 0, sizeof(siz));
    memset(mx, 0, sizeof(mx));
    ans = 0;    
}
void solve() {

    N = read(); M = read();
    init();
    scanf("%s", a + 1);
    for(int i = 1; i <= N - 1; i++) {
        int x = read(), y = read();
        v[x].push_back(y); v[y].push_back(x);
    }
    for(int i = 1; i <= N; i++) reverse(v[i].begin(), v[i].end());
    scanf("%s", b + 1); po[0] = 1;
    for(int i = 1; i <= N; i++) {
        hp[i] = hp[i - 1] + b[(i - 1) % M + 1] * po[i - 1];
        hs[i] = hs[i - 1] + b[M - (i - 1) % M] * po[i - 1];
        po[i] = base * po[i - 1];
    }
    Siz = N; mx[0] = INF; Root = 0; FindRoot(1, 0);
    work(1); 
    printf("%d\n", ans);
}
signed main() {
    freopen("a.in", "r", stdin);
    for(int T = read(); T; T--, solve());
    return 0;
}

编程开发网
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇BZOJ5462: [APIO2018] 新家(K-D T.. 下一篇BZOJ4650: [Noi2016]优秀的拆分(h..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

array(4) { ["type"]=> int(8) ["message"]=> string(24) "Undefined variable: jobs" ["file"]=> string(32) "/mnt/wp/cppentry/do/bencandy.php" ["line"]=> int(214) }