Problem Palindromes and Super Abilities 2 (URAL2040)

题目大意

  给一个字符串,从左到右依次添加,询问每添加一个字符,新增加的回文串数量。

解题分析

  用回文自动机来做,如果新添加了一个字符,自动机中新开了一个节点,说明新增加了一个回文串。

  对于每新添加一个字符,新增加的回文串数量最多为1。

  另外,这道题既卡空间又卡时间,交了n发才过。

参考程序

 #include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <string>
#include <vector>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#pragma comment(linker,"/STACK:102400000,102400000")
using namespace std; #define N 5000008
#define V 1008
#define E 60008
#define lson l,m,rt<<1
#define rson m,r+1,rt<<1|1
#define clr(x,v) memset(x,v,sizeof(x));
#define LL long long const int mo = ;
const int inf = 0x3f3f3f3f;
const int INF = ;
/**************************************************************************/
int n,cnt,last;
int nt[N][],size[N],len[N],fail[N];
char s[N]; void init(){
fail[]=fail[]=;
len[]=-;
last=; cnt=;
}
void insert(int c,int n){
int p=last;
while (s[n-len[p]-]!=s[n]) p=fail[p];
if (!nt[p][c]){
int now=++cnt,k=fail[p];
len[now]=len[p]+;
while (s[n-len[k]-]!=s[n]) k=fail[k];
fail[now]=nt[k][c]; nt[p][c]=now;
putchar('');
}
else putchar("");
last=nt[p][c];
size[last]++;
} void solve(){
for (int i=cnt;i>=;i--) size[fail[i]]+=size[i];
}
int main(){
gets(s+);
init();
n=strlen(s+);
for (int i=;i<=n;i++) insert(s[i]-,i);
}

URAL 2040 (回文自动机)的更多相关文章

  1. Ural 2040. Palindromes and Super Abilities 2 回文自动机

    2040. Palindromes and Super Abilities 2 题目连接: http://acm.timus.ru/problem.aspx?space=1&num=2040 ...

  2. URAL 2040 Palindromes and Super Abilities 2 (回文自动机)

    Palindromes and Super Abilities 2 题目链接: http://acm.hust.edu.cn/vjudge/contest/126823#problem/E Descr ...

  3. 回文树(回文自动机) - URAL 1960 Palindromes and Super Abilities

     Palindromes and Super Abilities Problem's Link: http://acm.timus.ru/problem.aspx?space=1&num=19 ...

  4. 后缀自动机/回文自动机/AC自动机/序列自动机----各种自动机(自冻鸡) 题目泛做

    题目1 BZOJ 3676 APIO2014 回文串 算法讨论: cnt表示回文自动机上每个结点回文串出现的次数.这是回文自动机的定义考查题. #include <cstdlib> #in ...

  5. [模板] 回文树/回文自动机 && BZOJ3676:[Apio2014]回文串

    回文树/回文自动机 放链接: 回文树或者回文自动机,及相关例题 - F.W.Nietzsche - 博客园 状态数的线性证明 并没有看懂上面的证明,所以自己脑补了一个... 引理: 每一个回文串都是字 ...

  6. BZOJ2160拉拉队排练——回文自动机

    题目描述 艾利斯顿商学院篮球队要参加一年一度的市篮球比赛了.拉拉队是篮球比赛的一个看点,好的拉拉队往往能帮助球队增加士气,赢得最终的比赛.所以作为拉拉队队长的楚雨荨同学知道,帮助篮球队训练好拉拉队有多 ...

  7. BZOJ2084[Poi2010]Antisymmetry——回文自动机

    题目描述 对于一个01字符串,如果将这个字符串0和1取反后,再将整个串反过来和原串一样,就称作“反对称”字符串.比如00001111和010101就是反对称的,1001就不是.现在给出一个长度为N的0 ...

  8. BZOJ2342[Shoi2011]双倍回文——回文自动机

    题目描述 输入 输入分为两行,第一行为一个整数,表示字符串的长度,第二行有个连续的小写的英文字符,表示字符串的内容. 输出 输出文件只有一行,即:输入数据中字符串的最长双倍回文子串的长度,如果双倍回文 ...

  9. 【XSY2715】回文串 树链剖分 回文自动机

    题目描述 有一个字符串\(s\),长度为\(n\).有\(m\)个操作: \(addl ~c\):在\(s\)左边加上一个字符\(c\) \(addr~c\):在\(s\)右边加上一个字符 \(tra ...

随机推荐

  1. 正则表达式 (C++)

    本主题讨论各正则表达式引擎的语法. 正则表达式语法 语法摘要 语义详细信息 匹配和搜索 格式标志 正则表达式语法   元素  元素可以是下列项之一: 一般字符,可匹配目标序列中的相同字符. 通配符“. ...

  2. mysql 添加字段、删除字段、调整字段顺序 转

    ALTER TABLE — 更改表属性添加字段: alter table `user_movement_log`Add column GatewayId int  not null default 0 ...

  3. java 中遇到的问题及解决方法

    1.经常发现明明导入jar包,还是会报java.lang.NoSuchMethodError和java.lang.NoClassDefFoundError 试试网上的各种方法,包括重新导入jar包.重 ...

  4. Go语言并发与并行学习笔记(二)

    转:http://blog.csdn.net/kjfcpua/article/details/18265461 Go语言的并发和并行 不知道你有没有注意到一个现象,还是这段代码,如果我跑在两个goro ...

  5. C# webbrowser 修改useragent

    http://www.lukepaynesoftware.com/articles/programming-tutorials/changing-the-user-agent-in-a-web-bro ...

  6. SQL逻辑读变成零

    使用缓存HINT 让逻辑读变成0. create table t as select * from dba_objects; insert into t select * from t; commit ...

  7. DataGridView复选框实现单选功能(二)

    双击DataGridView进入事件 private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventA ...

  8. for循环往Oracle中插入n条数据,主键自增

    1.主键自增实现方法:http://www.cnblogs.com/Donnnnnn/p/5959871.html 2.for循环往Oracle中插入n条数据 BEGIN .. loop insert ...

  9. JavaScript原生对象属性和方法详解——Array对象

    http://www.feeldesignstudio.com/2013/09/native-javascript-object-properties-and-methods-array/ lengt ...

  10. 读写其他应用程序的SharedPreference

    2013-12-28 18:03:40 要读写其他应用的SharedPreference,前提是创建该SharedPreference的程序指定相应的可读或可写的权限, 如下: private voi ...