• ZOJ


    题意:给一个字符串,和每个字符代表的val,每个回文串的价值就是前半部分的val26进制%777777777,求价值第k小的回文串
    题解:建个pam,然后dfs两边(0,1),统计价值sort一遍就好了
    k爆int了,= =白wa了半天

    //#pragma GCC optimize(2)
    //#pragma GCC optimize(3)
    //#pragma GCC optimize(4)
    //#pragma GCC optimize("unroll-loops")
    //#pragma comment(linker, "/stack:200000000")
    //#pragma GCC optimize("Ofast,no-stack-protector")
    //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
    #include<bits/stdc++.h>
    #define fi first
    #define se second
    #define db double
    #define mp make_pair
    #define pb push_back
    #define pi acos(-1.0)
    #define ll long long
    #define vi vector<int>
    #define mod 777777777
    #define ld long double
    #define C 0.5772156649
    #define ls l,m,rt<<1
    #define rs m+1,r,rt<<1|1
    #define pll pair<ll,ll>
    #define pil pair<int,ll>
    #define pli pair<ll,int>
    #define pii pair<int,int>
    //#define cd complex<double>
    #define ull unsigned long long
    #define base 1000000000000000000
    #define Max(a,b) ((a)>(b)?(a):(b))
    #define Min(a,b) ((a)<(b)?(a):(b))
    #define fin freopen("a.txt","r",stdin)
    #define fout freopen("c.txt","w",stdout)
    #define fio ios::sync_with_stdio(false);cin.tie(0)
    template<typename T>
    inline T const& MAX(T const &a,T const &b){return a>b?a:b;}
    template<typename T>
    inline T const& MIN(T const &a,T const &b){return a<b?a:b;}
    inline void add(ll &a,ll b){a+=b;if(a>=mod)a-=mod;}
    inline void sub(ll &a,ll b){a-=b;if(a<0)a+=mod;}
    inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
    inline ll qp(ll a,ll b){ll ans=1;while(b){if(b&1)ans=ans*a%mod;a=a*a%mod,b>>=1;}return ans;}
    inline ll qp(ll a,ll b,ll c){ll ans=1;while(b){if(b&1)ans=ans*a%c;a=a*a%c,b>>=1;}return ans;}
    
    using namespace std;
    
    const double eps=1e-8;
    const ll INF=0x3f3f3f3f3f3f3f3f;
    const int N=100000+10,maxn=100000+10,inf=0x3f3f3f3f;
    
    char s[N];
    ll res,f[N],val[30];
    pll ans[N];
    struct PAM{
        int ch[N][26],fail[N],len[N],s[N];
        ll cnt[N];
        int last,n,p;
        int newnode(int w)
        {
            for(int i=0;i<26;i++)ch[p][i] = 0;
            cnt[p] = 0;
            len[p] = w;
            return p++;
        }
        void init()
        {
            p = last = n = 0;
            newnode(0);
            newnode(-1);
            s[n] = -1;
            fail[0] = 1;
        }
        int getfail(int x)
        {
            while(s[n-len[x]-1] != s[n]) x = fail[x];
            return x;
        }
        void add(int c)
        {
            s[++n] = c;
            int cur = getfail(last);
            if(!ch[cur][c]){
                int now = newnode(len[cur]+2);
                fail[now] = ch[getfail(fail[cur])][c];
                ch[cur][c] = now;
            }
            last = ch[cur][c];
            cnt[last]++;
        }
        void cal()
        {
            for(int i=p-1;i>=0;i--)cnt[fail[i]]+=cnt[i];
        }
        void dfs(int u,int len,ll v)
        {
            for(int i=0;i<26;i++)
                if(ch[u][i])
                {
                    ans[++res]=mp((v+f[len]*val[i]%mod)%mod,cnt[ch[u][i]]);
                    dfs(ch[u][i],len+1,(v+f[len]*val[i]%mod)%mod);
                }
        }
    }pam;
    int main()
    {
    //    fin;fout;
        f[0]=1;
        for(int i=1;i<N;i++)f[i]=f[i-1]*26ll%mod;
        int T;scanf("%d",&T);
        while(T--)
        {
            pam.init();
            int n,m;scanf("%d%d%s",&n,&m,s+1);
            for(int i=1;i<=n;i++)pam.add(s[i]-'a');
            pam.cal();
            while(m--)
            {
                ll k;scanf("%lld",&k);
                for(int i=0;i<26;i++)scanf("%lld",&val[i]);
                res=0;
                pam.dfs(0,0,0);pam.dfs(1,0,0);
    //            printf("%d
    ",res);
                sort(ans+1,ans+1+res);
                ll co=0;
                for(int i=1;i<=res;i++)
                {
                    co+=ans[i].se;
                    if(co>=k)
                    {
                        printf("%lld
    ",ans[i].fi);
                        break;
                    }
                }
            }
            puts("");
        }
        return 0;
    }
    /********************
    10
    78 3
    abaabbabababcddajkjdlspeoiabaabbabababcddajkjdlspeoiabaabbabababcddajkjdlspeoi
    23 25 25 22 23 24 25 16 17 18 19 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
    ********************/
    
  • 相关阅读:
    jsp 认知(2)
    jsp 认知
    Java 匿名类
    Usage of readonly and const
    Javascript 原型继承(续)—从函数到构造器的角色转换
    JavaScript 原型继承开端
    JS 函数调用
    Javascript Basic Operation Extraction
    JS单词形式的运算符
    git问题收集
  • 原文地址:https://www.cnblogs.com/acjiumeng/p/9774410.html
Copyright © 2020-2023  润新知