HYSBZ 2160 Cheerleading Rehearsal (palindrome tree)

Source: Internet
Author: User

2160: Cheerleading Rehearsal Time limit: ten Sec Memory Limit: 259 MB
Submit: 825 Solved: 324
[Submit] [Status] [Discuss] Description

The Ellis school basketball team is taking part in the annual City basketball game. Cheerleaders are a highlight of the basketball game, and good cheerleaders tend to help the team increase morale and win the final game. So as the cheerleader Captain Chu rain nettle students know, help the basketball team to train good cheerleaders is how important. Cheerleading selection work is over, in the rain nettle and the headmaster's choice, N-position set of excellent body, dance in one of the beautiful women from the numerous enrolment of the girls stand out. The girls will join the basketball team and compete with their opponents to cheer for the Eliston basketball team. A sunny morning, the rain nettle led the cheerleaders to start the rehearsal of the team. N Girls line up from left to right, and each person holds a sign written with 26 lowercase letters, waving in the game, shouting and cheering for the lads. Rain nettle found that if a continuous section of girls, there are odd numbers, and their hands of the sign written letters, from left to right and from right to left to read the same, then this section of the girl is called a harmonious small group. Now rain nettle want to find out all the harmonious small group, and according to the number of girls in descending order, the first K harmonious small group of girls number of the product is how much. Because the answer may be great, rain nettle as long as you tell her, the answer is divided by 19930726 of the remainder is OK.

Input

Enter as standard input. The first act, two positive integers n and K, represents something that has been described in the title description. The next line is n characters, representing the letters written on the sign from left to right.

Output

The output is standard output. Outputs an integer representing the remainder of the product divided by 19930726 in the description of the title, if the total number of harmonious small groups is less than K, output an integer-1.

Sample Input5 3
AbabaSample Output

45

Palindrome Tree Template

#include <iostream> #include <string.h> #include <stdlib.h> #include <stdio.h> #include < Math.h> #include <algorithm>using namespace std;typedef long long int ll;const int maxn=1e6+5;const LL mod=199307 26;char STR[MAXN]; LL n,k;int Cot;    LL sum;struct _link{int u[maxn];int V[MAXN];    int _next[maxn];int HEAD[MAXN];    int tot;        void Clear () {memset (head,-1,sizeof (head));    tot=0; } int get (int x,int y) {for (int i=head[x];i!=-1;i=_next[i]) {if (u[i]==y) r        Eturn V[i];    } return 0;        } void Insert (int x,int y,int z) {u[tot]=y; v[tot]=z;        _NEXT[TOT]=HEAD[X];    head[x]=tot++;    }};struct node{int len; LL num;}    C[maxn];struct tree{_link _next;    int FAIL[MAXN];    int LEN[MAXN];    LL CNT[MAXN];    int S[MAXN];    int last;    int p;    int n;        int new_node (int x) {cnt[p]=0;        Len[p]=x;    return p++; } void Init ()   {_next.clear ();        P=0;        New_node (0);        New_node (-1);        last=0;        n=0;        S[0]=-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 x) {x-= ' a ';        S[++n]=x;        int Cur=get_fail (last); if (! (            Last=_next.get (cur,x)) {int Now=new_node (len[cur]+2);            Fail[now]=_next.get (Get_fail (Fail[cur]), x);            _next.insert (Cur,x,now);        Last=now;        } cnt[last]++;    return 1;    } void Count () {for (int i=p-1;i>=0;i--) cnt[fail[i]]+=cnt[i];        } LL Fun () {count (); cot=0;        LL sum=0; for (int i=2;i<=p-1;i++) {if (!) (            len[i]&1)) continue;            C[cot].len=len[i],c[cot++].num=cnt[i];        Sum+=cnt[i];    } return sum; }}tree;int CMP (Node A,node b) {return a.len>b.len;} ll Quick (ll N,ll x) {LL sum=1;        for (x;x;x>>=1) {if (x&1) sum= (sum*n)%mod;    N= (n*n)%mod; } return sum;}        int main () {while (scanf ("%lld%lld", &n,&k)!=eof) {scanf ("%s", str);        Tree.init ();        for (int i=0;i<n;i++) Tree.add (Str[i]);        Sum=tree.fun ();        Sort (c,c+cot,cmp);            if (sum<k) {printf (" -1\n");        Continue        } LL Ans=1; for (int i=0;i<cot;i++) {if (k>c[i].num) {ans= (Ans*quick (C[I].LEN,C[I].N                UM))%mod;            K-=c[i].num;                 } else {ans= (Ans*quick (c[i].len,k))%mod;            k=0;        } if (k==0) break;    } printf ("%lld\n", ans); } return 0;}


HYSBZ 2160 Cheerleading Rehearsal (palindrome tree)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.