Finding Palindromes - 猥琐的字符串(Manacher+trie)
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std; const int MAXN = 2e6+;
const int oo = 1e9+; struct Trie
{///字典树的节点
int next[];
int leaf;///以这个节点为终点的叶子节点个数
int count;///后面回文串的数量 void Free()
{
leaf = count = ;
memset(next, false, sizeof(next));
} }trie[MAXN]; int top;///配合字典树使用,top表示没有使用的内存
int start[MAXN], p[MAXN<<];
char MumStr[MAXN], Estr[MAXN<<];
bool suffix[MAXN];///后缀是否是回文串
bool prefix[MAXN];///前缀是否是回文串 void BuildTrie(int n)
{
int p = ; for(int i=start[n+]-; i>=start[n]; i--)
{///把字符串倒插进去
int k = MumStr[i] - 'a'; trie[p].count += prefix[i]; if(trie[p].next[k] == )
{
trie[p].next[k] = ++top;
trie[top].Free();
}
p = trie[p].next[k];
} trie[p].leaf += ;
}
void Manacher(int n)
{
int i, id=, len=; Estr[] = '$'; for(i=start[n]; i<start[n+]; i++)
{
Estr[len++] = '#';
Estr[len++] = MumStr[i]; suffix[i] = false;
prefix[i] = false;
}
Estr[len] = '#';
Estr[len+] = ; for(i=; i<len; i++)
{
p[i] = ; if(p[id]+id > i)
p[i] = min(p[id*-i], p[id]+id-i); while(Estr[ i+p[i] ] == Estr[ i-p[i] ])
p[i]++; if(p[id]+id < p[i]+i)
id = i; if(p[i] == i)
prefix[ start[n]+p[i]- ] = true;
if(p[i]+i- == len)
suffix[ start[n+]-p[i]+ ] = true;
}
}
int Query(int n)
{
int i, p=, sum = ; for(i=start[n]; i<start[n+]; i++)
{
int k = MumStr[i] - 'a'; if(trie[p].next[k] == )
break; p = trie[p].next[k]; if(suffix[i+] || i==start[n+]-)
sum += trie[p].leaf;
} if(i == start[n+])
sum += trie[p].count; return sum;
} int main()
{
int N; while(scanf("%d", &N) != EOF)
{
int i, len; top = ;
trie[].Free(); for(i=; i<=N; i++)
{
scanf("%d%s", &len, MumStr+start[i]);
start[i+] = start[i] + len; Manacher(i);
BuildTrie(i);
} long long ans = ; for(i=; i<=N; i++)
ans += Query(i); printf("%lld\n", ans);
} return ;
}
/**
2
3 abc
4 acba */
Finding Palindromes - 猥琐的字符串(Manacher+trie)的更多相关文章
- POJ 3376 Finding Palindromes(manacher求前后缀回文串+trie)
题目链接:http://poj.org/problem?id=3376 题目大意:给你n个字符串,这n个字符串可以两两组合形成n*n个字符串,求这些字符串中有几个是回文串. 解题思路:思路参考了这里: ...
- poj3376 Finding Palindromes【exKMP】【Trie】
Finding Palindromes Time Limit: 10000MS Memory Limit: 262144K Total Submissions:4710 Accepted: 8 ...
- POJ3376 Finding Palindromes —— 扩展KMP + Trie树
题目链接:https://vjudge.net/problem/POJ-3376 Finding Palindromes Time Limit: 10000MS Memory Limit: 262 ...
- POJ 3376 Finding Palindromes(扩展kmp+trie)
题目链接:http://poj.org/problem?id=3376 题意:给你n个字符串m1.m2.m3...mn 求S = mimj(1=<i,j<=n)是回文串的数量 思路:我们考 ...
- poj 3376 Finding Palindromes
Finding Palindromes http://poj.org/problem?id=3376 Time Limit: 10000MS Memory Limit: 262144K ...
- PHP后门新玩法:一款猥琐的PHP后门分析
0x00 背景 近日,360网站卫士安全团队近期捕获一个基于PHP实现的webshell样本,其巧妙的代码动态生成方式,猥琐的自身页面伪装手法,让我们在分析这个样本的过程中感受到相当多的乐趣.接下来就 ...
- 模板—字符串—Manacher
模板—字符串—Manacher Code: #include <cstdio> #include <cstring> #include <algorithm> us ...
- delphi之猥琐的webserver实现
http://www.birdol.com/cainiaobiancheng/238.html delphi之猥琐的webserver实现 菜鸟编程 十五楼的鸟儿 7年前 (2009-01-01) ...
- 牛客练习赛11 假的字符串 (Trie树+拓扑找环)
牛客练习赛11 假的字符串 (Trie树+拓扑找环) 链接:https://ac.nowcoder.com/acm/problem/15049 来源:牛客网 给定n个字符串,互不相等,你可以任意指定字 ...
随机推荐
- 330. Patching Array--Avota
问题描述: Given a sorted positive integer array nums and an integer n, add/patch elements to the array s ...
- topcoder算法练习2
Problem Statement In most states, gamblers can choose from a wide variety of different lottery ...
- win7访问windows server 2003服务器出现未知的用户名或者错误的密码(转载)
直接放答案,感谢网友提供答案,否则自已还一直在纳闷,为什么? win7系统的安全机制限制了登陆.只要系统时间和win2003服务器的系统时间相差很多,系统就会阻止其登陆,并显示错误信息:"未 ...
- Javascript跳转手机站代码
$(document).ready(function(){ var mobileAgent = new Array("iphone", "ipod", &quo ...
- BAE 环境下 hibernate annotations 配置
annotations 配置 首先需要加入 hibernate-jpa-2.0-api-1.0.1.Final.jar 和 ejb3-persistence.jar 这两个包 ejb3-persis ...
- 在apache上报错“The _imaging C module is not installed”
我的环境是python2.7.8.django1.6.4.apache2.2. 问题:在django自带的runserver环境下没有任何报错,但是配置在apache上出现了 “The _imagin ...
- 十月例题F题 - City Game
F - City Game Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Bob is a st ...
- ECommon.Dapper
ECommon.Dapper 轻量级的dapper扩展 我们都知道Dapper这个orm框架,但是我们也知道他的扩展目前没有特别好的,今天我就推荐一个轻量级的很方便使用的一个扩展叫做 ECommon. ...
- jquery.js 库中的 选择器
<html><head><script type="text/javascript" src="jquery.js">< ...
- HTML5中的 DOM 树
DOM (文档对象模型(Document Object Model)) 文档对象模型(Document Object Model,简称DOM),是W3C组织推荐的处理可扩展标志语言的标准编程接口.Do ...