回文树练习

回文树的题目一般只有几种类型,主要是应用cnt,num

1.ural1960. Palindromes and Super Abilities

http://acm.timus.ru/problem.aspx?space=1&num=1960

题意:每加入一个字符,询问不同回文串种类

题解:每次调用add后,输出p(节点个数)即可

代码:

#include
#include
#include
#include
using namespace std;

const int maxn = 1e5+10;
const int ALP = 26;

struct PAM{
    int next[maxn][ALP];
    int fail[maxn],cnt[maxn],len[maxn],num[maxn];
    int s[maxn],n,p,last;

    int newnode(int le){
        for(int i=0;i=0;i--)
            cnt[fail[i]] += cnt[i];
    }
}pam;

char s[maxn];
int main(){
    while(scanf("%s",s)!=EOF){
        int len = strlen(s);
        pam.init();
        for(int i=0;i0) printf(" ");
            printf("%d",he);
        }
        puts("");
    }
    return 0;
}

2.TsinsenA1280. 最长双回文串

http://www.tsinsen.com/A1280

题意:找到最长的串XY,X为长度非0的回文串,Y为长度非0的回文串。

题解:对于每一个下标i,找到以i为结尾的最长回文长度和以i为开头的最长回文长度。

可以将原串正着做一次,再反着做一次

代码:

#include
#include
#include
#include
using namespace std;
typedef long long ll;

const int maxn = 100005;
const int ALP = 26;

struct PAM{
    int next[maxn][ALP];
    int fail[maxn];
    int cnt[maxn];
    int num[maxn];
    int len[maxn];
    int s[maxn];
    int last;
    int n;
    int p;

    int newnode(int w){
        for(int i=0;i=0;i--)
            cnt[fail[i]] += cnt[i];
    }
}pam;

char s[maxn];
int f[maxn];
int main(){
    scanf("%s",s);
    int len = strlen(s);
    pam.init();
    for(int i=0;i

3.TsinsenA1255. 拉拉队排练

http://www.tsinsen.com/A1255

#include
#include
#include
#include
using namespace std;
typedef long long ll;
const ll mod = 19930726;
const int maxn = 1e6+10;
const int ALP = 26;
ll k_pow(ll a,ll n){
    ll ret = 1ll;
    ll mo = a;
    while(n){
        if(n&1) ret = (ret * mo)%mod;
        mo = (mo * mo)%mod;
        n /= 2;
    }
    return ret;
}
struct PAM{
    int next[maxn][ALP];
    int fail[maxn];
    int len[maxn];
    ll cnt[maxn];
    ll num[maxn];
    int s[maxn];
    ll co[maxn];
    ll sum;
    int p,n,last;
    int newnode(int le){
        for(int i=0;i=0;i--){
            cnt[fail[i]] += cnt[i];
            if(len[i]>0) co[len[i]] += cnt[i];
            if(len[i]>0 && (len[i]&1)) sum += cnt[i];
        }
    }
    ll solve(int nu,ll k){
        if(k>sum) return -1;
        ll ret = 1ll;
        for(int i=nu;i>=1;i--)if(k>0&&(i&1)&&co[i]>0){
            if(co[i]>n>>k){
        scanf("%s",s);
        pam.init();
        for(int i=0;i

4.TsinsenA1393. Palisection

http://www.tsinsen.com/A1393

会超空间,所以用邻接表存储。

#include
#include
#include
#include
#include
using namespace std;
typedef long long ll;
const ll mod = 51123987;
const int maxn = 2*1e6+10;
const int ALP = 26;

struct PAM{
    vector > next[maxn];
    int fail[maxn],num[maxn],len[maxn];
    ll cnt[maxn];
    int s[maxn],n,p,last,sum;

    int newnode(int w){
        next[p].clear();
        cnt[p]=num[p]=0;
        len[p]=w;
        return p++;
    }
    void init(){
        p = 0;
        sum = 0;
        newnode(0);
        newnode(-1);
        last = 0;
        n = 0;
        s[n] = -1;
        fail[0] = 1;
    }
    int get_fail(int x){
        while(s[n-len[x]-1] != s[n]) x = fail[x];
        return x;
    }
    int add(int c){
        c -= 'a';
        s[++n] = c;
        int cur = get_fail(last);
        int flag = 0;
        for(int i=0;i1;i--){
            cnt[fail[i]] += cnt[i];
        }
    }
}pam;

ll exgcd(ll a,ll b,ll &x,ll &y){
    ll d=a;
    if(b == 0){
       x = 1ll;y = 0;
    }  else{
        d=exgcd(b,a%b,y,x);
        y-=(a/b)*x;
    }
    return d;
}
ll inv(ll a){
    ll x,y;
    exgcd(a,mod,x,y);//mod
    return (x+mod)%mod;
}


ll dp[maxn];
char s[maxn];
int main(){
    int n; scanf("%d",&n);
    scanf("%s",s);
    pam.init();
    for(int i=0;i0) dp[i] += dp[i-1];
        dp[i] %= mod;
    }
    ll sum = dp[n-1];
    reverse(s,s+n);
    ll ans = 0;
    pam.init();
    for(int i=0;i

5.2014-2015 ACM-ICPC, Asia Xian Regional Contest G The Problem to Slow Down You(uva 7041)

话说去年考了,今年到底考不考?

按照相同路径走道的节点一定是相同的回文串,所以分别以0和1为起点,跑两边dfs

#include
#include
#include
#include
using namespace std;
const int maxn = 200010*2;
const int ALP = 26;
typedef long long ll;

struct PAM{
    int next[maxn][ALP];
    int fail[maxn];
    int len[maxn];
    int num[maxn];
    int cnt[maxn];
    int s[maxn];
    int last,n,p;

    int newnode(int le){
        for(int i=0;i=0;i--)
            cnt[fail[i]] += cnt[i];
    }
}pam1,pam2;
ll dfs(int an,int bn){
    ll ret = 0;
    for(int i=0;i>t;
    while(t--){
        scanf("%s%s",s1,s2);
        pam1.init();
        pam2.init();
        int len1 = strlen(s1) , len2 = strlen(s2);
        for(int i=0;i

6.bzoj 3676

http://www.lydsy.com/JudgeOnline/problem.php?id=3676

模板题放到了最后,不好意思

cnt*len统计最大值即可

#include
#include
#include
#include
using namespace std;

const int maxn = 300010*2;
const int ALP = 26;

struct PAM{
    int next[maxn][ALP];
    int fail[maxn];
    int cnt[maxn];
    int num[maxn];
    int len[maxn];
    int s[maxn];
    int last,n,p;

    int newnode(int l){
        for(int i=0;i=0;i--)
            cnt[fail[i]] += cnt[i];
    }
}pam;

char s[maxn];
int main(){
    scanf("%s",s);
    int len = strlen(s);
    pam.init();
    for(int i=0;i


你可能感兴趣的:(字符串)