Victor and String

来源:互联网 发布:北京网站排名优化 编辑:程序博客网 时间:2024/06/14 18:33

题目描述

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.

输入描述:

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 $op$, meaning the type of operation. If $op$=$1$ or $2$, there will be a lowercase English letters followed.       $1\leq n \leq 100000$.

输出描述:

For each query operation(operation 3 or 4), print the correct answer.
示例1

输入

61 a1 b2 a2 c3481 a2 a2 a1 a31 b34

输出

4545

11

#include <stdio.h>#include <string.h>#include <algorithm>using namespace std ; typedef long long LL ; #define clr( a , x ) memset ( a , x , sizeof a ) const int MAXN = 200005 ; struct Palindromic_Tree {    int nxt[MAXN][26] ;    int fail[MAXN] ;    int len[MAXN] ;    int S[MAXN] , L , R ;    int num[MAXN] ;    int last[2] ;    int p ;    LL tot ;     int newnode ( int l ) {        for ( int i = 0 ; i < 26 ; ++ i ) nxt[p][i] = 0 ;        num[p] = 0 ;        len[p] = l ;        return p ++ ;    }     void init ( int n ) {        p = 0 ;        newnode ( 0 ) ;        newnode ( -1 ) ;         clr ( S , -1 ) ;        L = n ;        R = n - 1 ;        last[0] = last[1] = 1 ;        fail[0] = 1 ;         tot = 0 ;    }     int get_fail ( int v , bool d ) {        if ( d ) while ( S[R - len[v] - 1] != S[R] ) v = fail[v] ;        else while ( S[L + len[v] + 1] != S[L] ) v = fail[v] ;        return v ;    }     void add ( int c , bool d ) {        if ( d ) S[++ R] = c ;        else S[-- L] = c ;        int cur = get_fail ( last[d] , d ) ;        if ( !nxt[cur][c] ) {            int now = newnode ( len[cur] + 2 ) ;            fail[now] = nxt[get_fail ( fail[cur] , d )][c] ;            nxt[cur][c] = now ;            num[now] = num[fail[now]] + 1 ;        }        last[d] = nxt[cur][c] ;        if ( len[last[d]] == R - L + 1 ) last[d ^ 1] = last[d] ;        tot += num[last[d]] ;    }} ; Palindromic_Tree T ;int n ; void solve () {    int op ;    char c ;    T.init ( n ) ;    for ( int i = 0 ; i < n ; ++ i ) {        scanf ( "%d" , &op ) ;        if ( op <= 2 ) {            scanf ( " %c" , &c ) ;            T.add ( c - 'a' , op - 1 ) ;        }        if ( op == 3 ) printf ( "%d\n" , T.p - 2 ) ;        if ( op == 4 ) printf ( "%I64d\n" , T.tot ) ;    }} int main () {    while ( ~scanf ( "%d" , &n ) ) solve () ;    return 0 ;}


原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 葫芦干了有黑点怎么办 单位费用发票丢失了怎么办 1岁宝宝太调皮了怎么办 孩子和同学打架家长该怎么办 孩子被大人打了怎么办 小孩不跟大人沟通怎么办 2岁小朋友爱动手怎么办 小班爱动手的小朋友怎么办 宝宝对小朋友不友好爱动手怎么办 小孩子上幼儿园爱打人怎么办 小孩被别人打了怎么办 1岁幼儿爱打人怎么办 作为幼小朋友打人老师怎么办 2岁半小朋友喜欢打人怎么办 2岁宝宝脾气大怎么办 4月小孩爱动怎么办 一岁宝宝老打人怎么办 1岁宝宝爱打人怎么办 3岁宝宝喜欢抓人怎么办 宝宝喜欢打人怎么办2岁 1岁宝宝动手打人怎么办 孩子总打人总哭怎么办 小孩出现夜惊家人怎么办 小孩不原跟家人沟通怎么办 孩子字写得难看怎么办 孩子上一年级不认识字怎么办 二年级孩子语文差怎么办 孩子二年级语文成绩差怎么办 孩子小学二年级语文差怎么办 二年级孩子语文理解能力差怎么办 深圳租房被坑了怎么办 小鸣单车押金退不了怎么办 联想台式一体机忘记密码怎么办 ps直线工具变成箭头了怎么办 笔记本图形处理速度慢怎么办 微信语音发不出去怎么办 ps里的图层锁定怎么办 ps图层丢失了怎么办 PS标题画面太小怎么办 轮胎蹭掉一块皮怎么办 吃香蕉吃的胃难受怎么办