zoukankan      html  css  js  c++  java
  • P4199 万径人踪灭 FFT + manacher

    (color{#0066ff}{ 题目描述 })

    (color{#0066ff}{输入格式})

    一行,一个只包含a,b两种字符的字符串

    (color{#0066ff}{输出格式})

    一行,一个整数表示问题的答案

    (color{#0066ff}{输入样例})

    abaabaa
    
    aaabbbaaa
    
    aaaaaaaa
    

    (color{#0066ff}{输出样例})

    14
        
    44
        
    53
    

    (color{#0066ff}{数据范围与提示})

    (color{#0066ff}{ 题解 })

    对于题目中的两个条件,首先我们不考虑是否连续,只考虑是否对称

    那么我们对于每一个对称轴,在两边找刚好位置对称字符相同的位置对数,看看有几对

    每个位置选或不选(不考虑是否连续),就是2的这么多次方

    暴力找显然是(O(n^2))的,我们考虑优化这个过程

    对于每个对称轴,答案为(sum [i和pos*2-i是否匹配])

    这两个位置相加为定值!

    考虑用FFT优化这个过程

    先构造函数,因为涉及a, b,不太好操作,我们把a,b分开处理

    构造(f_i= left{egin{aligned}0 s_i =b \ 1 s_i = aend{aligned} ight.)

    因为我们本就是对称匹配,所以序列不用翻转,直接FFT就行

    对b同理操作,把两次的多项式加起来,得到的就是每个对称轴对称的对数

    +1在/2就是总数,然后让他2的这么多次方再-1(因为不能为空)

    之后我们考虑连续的

    连续的,当且仅当以对称轴为中心,两边连续延伸出一些,没有空的(要连续)

    这个的方案数是什么呢? 显然这是个回文串,回文串的半径即为所求

    于是qwq。。。manacher啊

    把上面的ans在减去每个位置的回文半径就是ans了

    Code

    #include<bits/stdc++.h>
    #define LL long long
    LL in() {
        char ch; LL x = 0, f = 1;
        while(!isdigit(ch = getchar()))(ch == '-') && (f = -f);
        for(x = ch ^ 48; isdigit(ch = getchar()); x = (x << 1) + (x << 3) + (ch ^ 48));
        return x * f;
    }
    const int mox = 1e9 + 7;
    const int mod = 998244353;
    const int maxn = 4e5 + 10;
    int r[maxn], len, R[maxn];
    using std::vector;
    LL ksm(LL x, LL y, LL p) {
        LL re = 1LL;
        while(y) {
            if(y & 1) re = re * x % p;
            x = x * x % p;
            y >>= 1;
        }
        return re;
    }
    void FNTT(vector<int> &A, int flag) {
        A.resize(len);
        for(int i = 0; i < len; i++) if(i < r[i]) std::swap(A[i], A[r[i]]);
        for(int l = 1; l < len; l <<= 1) {
            int w0= ksm(3, (mod - 1) / (l << 1), mod);
            for(int i = 0; i < len; i += (l << 1)) {
                int w = 1, a0 = i, a1 = i + l;
                for(int k = 0; k < l; k++, a0++, a1++, w = 1LL * w0 * w % mod) {
                    int tmp = 1LL * A[a1] * w % mod;
                    A[a1] = ((A[a0] - tmp) % mod + mod) % mod;
                    A[a0] = (A[a0] + tmp) % mod;
                }
            }
        }
        if(!(~flag)) {
            std::reverse(A.begin() + 1, A.end());
            int inv = ksm(len, mod - 2, mod);
            for(int i = 0; i < len; i++) A[i] = 1LL * inv * A[i] % mod;
        }
    }
    vector<int> operator * (vector<int> A, vector<int> B) {
        int tot = A.size() + B.size() - 1;
        FNTT(A, 1), FNTT(B, 1);
        vector<int> ans;
        ans.resize(len);
        for(int i = 0; i < len; i++) ans[i] = 1LL * A[i] * B[i] % mod;
        FNTT(ans, -1);
        ans.resize(tot);
        return ans;
    }
    char s[maxn], t[maxn];
    void manacher(int L) {
    	int maxright = 0, pos = 0;
    	for(int i = 0; i < L; i++) {
    		if(i < maxright) R[i] = std::min(maxright - i, R[(pos << 1) - i]);
    		else R[i] = 1;
    		while(i + R[i] < L && i - R[i] >= 0 && t[i + R[i]] == t[i - R[i]]) R[i]++;
    		if(i + R[i] - 1 > maxright) maxright = i + R[i] - 1, pos = i;
    	}
    }
    int main() {
    	scanf("%s", s);
    	int L = strlen(s);
    	vector<int> a, b, c;
    	for(int i = 0; i < L; i++) a.push_back(s[i] == 'a');
    	for(len = 1; len <= L + L; len <<= 1);
    	for(int i = 0; i < len; i++) r[i] = (r[i >> 1] >> 1) | ((i & 1) * (len >> 1));
    	b = a * a;
    	a.clear();
    	for(int i = 0; i < L; i++) a.push_back(s[i] == 'b');
    	c = a * a;
    	for(int i = 0; i < len; i++) c[i] += b[i];
    	for(int i = 0; i < L; i++) {
    		t[i << 1] = s[i];
    		t[(i << 1) + 1] = '&';
    	}
    	L <<= 1;
    	manacher(L);
    	LL ans = 0;
    	for(int i = 0; i < L; i++) c[i] = (c[i] + 1) >> 1;
    	for(int i = 0; i < L; i++) {
    		ans = (ans + ksm(2, c[i], mox)) % mox;
    		ans = ((ans - 1 - (((R[i] + (!(i & 1))) >> 1))) % mox + mox) % mox;
    	}
    	printf("%lld", ans);
        return 0;
    }
    
  • 相关阅读:
    Drupal忘记管理员密码
    Drupal7新装一个主题时页面白屏,如何设置一个默认主题?
    Drupal7强制把主题恢复到默认主题
    Drupal常用的模块
    网页流量分析系统
    S运算符&&和|| 及其优先级
    Linux crontab命令
    C语言实现文件实时更新
    Linux 查看设置系统语言
    Python沙盒环境配置
  • 原文地址:https://www.cnblogs.com/olinr/p/10273513.html
Copyright © 2011-2022 走看看