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.
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
输出
454511
#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 ;}
阅读全文
0 0
- HDU5421-Victor and String
- Victor and String
- HDU 5421 Victor and String
- [HDU5421]Victor and String-回文树
- HDOJ 5421 Victor and String 回文串自动机
- HDU 5421 Victor and String(回文树)
- Victor and Machine
- 【hdu5419】Victor and Toys
- Victor and Machine
- hdu 5417 Victor and Machine
- hdu 5417 Victor and Machine
- HDU 5417 Victor and Machine
- HDU-5417Victor and Machine
- hdu 5417 Victor and Machine
- hdu 5419 Victor and Toys
- HDOJ 5417 Victor and Machine
- hdu 5417 Victor and Machine
- BestCoder#52 Victor and World
- 动态更换app启动图标
- ConnectTimeout和ReadTimeout所代表的意义
- 读《锋利的JQUERY》读书笔记
- 使用提交表单及上传文件(适用于aspx)
- 数据结构实验之查找三:树的种类统计
- Victor and String
- [nodejs]excel表格导出
- 获得绝对路径
- 给定一个填充非负数的m×n网格,找到一条从左上到右下的路径,这个路径将所有数字的总和最小化。
- 简单易懂的状态栏使用详解(2)---状态栏改颜色
- Android PullToRefresh 完全解析
- java里面String,Date,TimeStamp之间的互转
- C#设计模式(9)——装饰者模式(Decorator Pattern)
- 1273 面向对象程序设计上机练习十一(运算符重载)