codeforces-557e (trie

Source: Internet
Author: User

Title: Give a series of only AB, find the half palindrome of the K large.

Idea: Violence to cite all half palindrome string, and then violence into the trie query K large, originally written trie is copy template, this time I wrote a bit.

#include <iostream>#include<map>#include<algorithm>#include<cstdio>#include<cstring>#include<cstdlib>#include<vector>#include<queue>#include<stack>#include<functional>#include<Set>#include<cmath>#definePB Push_back#defineFS First#defineSe Second#defineSq (x) (x) * (x)#defineEPS 0.0000000001#defineIinf (1&LT;&LT;30)using namespaceStd;typedefLong LongLl;typedef pair<ll,ll>P;Const intCharsize=2;Const intmaxv=5005;Const intMaxpool=25000000;BOOLISPA[MAXV][MAXV];CharSTR[MAXV];intlen,k;structnode{intnum; intsum; Node*Ch[charsize]; Node (): Num (0), SUM (0){};} Pool[maxpool]; Node*NULL=NewNode ();intPoolh=0; Node*NewNode () {Node*n=&pool[poolh++];  for(intI=0; i<charsize;i++) n->ch[i]=NULL; N->sum=n->num=0; returnN;} Node*root=NewNode ();voidInsert (node* N,intPints) {     while(p<Len) {        if(n->ch[str[p]-'a']==NULL) n->ch[str[p]-'a']=NewNode (); if(Ispa[s][p]) n->ch[str[p]-'a']->num++; N=n->ch[str[p]-'a'],p++; }}voidCul (Node *N) {    if(n==NULL)return; N->sum=n->num;  for(intI=0; i<charsize;i++) {Cul (n-Ch[i]); N->sum+=n->ch[i]->sum; }}voidOutput (Node *n,intk) {    if(n==NULL|| k<=0)return; intsum=0;  for(intI=0; i<charsize;i++){        if(sum+n->ch[i]->sum>=k) {printf ("%c",(Char)('a'+i)); Output (n->ch[i],k-sum-n->ch[i]->num); return; } Sum+=n->ch[i]->sum; }    return;}voidgetpa () { for(intI=0; i<len;i++){         for(intj=0; j<=len;j+=2){            if(i-j<0|| i+j>=Len) {                 Break; }            if(str[i+j]==str[i-J]) {Ispa[i-j][i+j]=1; }Else{                 Break; }        }         for(intj=1; j<=len;j+=2){            if(i-j<0|| i+j>=Len) {                 Break; }            if(str[i+j]==str[i-J]) {Ispa[i-j][i+j]=1; }Else{                 Break; }        }         for(intj=0; j<=len;j+=2){            if(i-j<0|| i+j+1>=len) { Break;} if(str[i-j]==str[i+j+1]) {ispa[i-j][i+j+1]=1; }Else{                 Break; }        }         for(intj=1; j<=len;j+=2){            if(i-j<0|| i+j+1>=len) { Break;} if(str[i-j]==str[i+j+1]) {ispa[i-j][i+j+1]=1; }Else{                 Break; }        }    }}intMain () {Freopen ("/home/files/cppfiles/in","R", stdin); /*Std::ios::sync_with_stdio (FALSE); Std::cin.tie (0);*/scanf ("%s", str); Len=strlen (str); CIN>>K;    GETPA ();  for(intI=0; i<len;i++) Insert (root,i,i);    Cul (root);    Output (root,k); return 0;}
View Code

codeforces-557e (trie

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: 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.