HDU - 5421 Victor and String (Bidirectional Insertion Palindrome)

Victor and String

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 524288/262144 K (Java/Others)
Total Submission(s): 680    Accepted Submission(s): 214


Problem Description
Victor loves to play with string. He thinks a string is charming as the string is a palindromic string.

Victor wants to play  n  times. Each time he will do one of following four operations.

Operation 1 : add a char  c  to the beginning of the string.

Operation 2 : add a char  c  to the end of the string.

Operation 3 : ask the number of different charming substrings.

Operation 4 : ask the number of charming substrings, the same substrings which starts in different location has to be counted.

At the beginning, Victor has an empty string.
 

Input
The input contains several test cases, at most  5  cases.

In each case, the first line has one integer  n  means the number of operations.

The first number of next  n  line is the integer  o p , meaning the type of operation. If  o p = 1  or  2 , there will be a lowercase English letters followed.

1n100000 .
 

Output
For each query operation(operation 3 or 4), print the correct answer.
 

Sample Input
 
  
6 1 a 1 b 2 a 2 c 3 4 8 1 a 2 a 2 a 1 a 3 1 b 3 4
 

Sample Output
 
  
4 5 4 5 11
 

Source
 

Recommend
hujie
 

Problem-solving idea: The palindrome tree can only handle one-way insertion, so it is necessary to expand the palindrome tree. It's very simple, just add a last pointer after the record is added forward. Then the details have to be worked out!


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 200005;
const int INF = 0x3f3f3f3f;

int len[MAXN];//Indicates the length of the palindrome represented by the node numbered i (one node represents a palindrome)
int nxt[MAXN][27];//Indicates the number of the palindrome string represented by the node number i after adding the character c on both sides to the palindrome string number
int fail[MAXN];//Indicates the longest suffix palindrome string of the palindrome string represented by node i that is not equal to itself after the node i mismatch
int num[MAXN];//Indicates the number of palindrome strings ending with the rightmost endpoint of the longest palindrome string represented by node i.
int cnt[MAXN];//Indicates the number of strings represented by node i
int lastR;
int lastL;
int L, R;
int S[MAXN];
int tot;//The total number of nodes, that is, the number of different palindrome strings, remember -2

int new_node(int l){
    cnt [tot] = 0;
    num [tot] = 0;
    len [tot] = l;
    return tot++;
}

void init_tree(int len1){
    L = len1;
    R=len1-1;
    memset(nxt,0,sizeof(nxt));
    to=0;
    new_node(0);
    new_node(-1);
    lastL=lastR=0;
    memset(S,-1,sizeof(S));
    fail[0]=1;
}

int get_fail(int x,bool d){
    if(d==1){
        while(S[R-len[x]-1]!=S[R])
            x=fail[x];
    }
    else{
        while(S[L+len[x]+1]!=S[L])
            x=fail[x];
    }
    return x;
}


int add_char(int c,bool d){
    c-='a';
    int cur;
    if(d==1){
        S[++R]=c;
        cur=get_fail(lastR,d);
    }
    else{
        S[--L]=c;
        cur=get_fail(lastL,d);
    }
    
    if(!nxt[cur][c]){
        int now=new_node(len[cur]+2);
        fail[now]=nxt[get_fail(fail[cur],d)][c];
        nxt[cur][c]=now;
        num[now]=num[fail[now]]+1;
    }
    if(d==1){
        lastR=nxt[cur][c];
        cnt[lastR]++;
        if(len[lastR]==R-L+1)//当添加字符时,可能会影响最后添加的那个字符,所以有时候要同步更新last
            lastL=lastR;
        return num[lastR];
    }
    else{
        lastL=nxt[cur][c];
        cnt[lastL]++;
        if(len[lastL]==R-L+1)
            lastR=lastL;
        return num[lastL];
    }
    
}

void count(){
    for(int i=tot-1;i>=0;i--)
        cnt[fail[i]]+=cnt[i];
    
    //    int ans=0;
    //    for(int i=2;i<tot;i++){
    //        ans+=cnt[i];
    //    }
    //    cout<<ans<<endl;
}
//void del(char pos)  
//{  
//    int x = pos - 'a';  
//    if (pre[tot])  
//    {  
//        next[pre[tot]][x] = 0;  
//    }  
//    --tot;  
//}

char str[MAXN];
char c[55];
int main(){
    int Q;
    while(~scanf("%d",&Q)){
        init_tree(Q);
        int op;
        ll sum=0;
        while(Q--){
            scanf("%d",&op);
            if(op==1){
                scanf("%s",c);
                sum+=ll(add_char(c[0],0));
            }
            if(op==2){
                scanf("%s",c);
                sum+=ll(add_char(c[0],1));
            }
            if(op==3){
                printf("%d\n",tot-2);
            }
            if(op==4){
                printf("%lld\n",sum);
            }
        }
        
    }
    
    
    
    
    return 0;
}





Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325989666&siteId=291194637