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个字符串,互不相等,你可以任意指定字 ...
随机推荐
- 常用CDN公共库
Jquery <script src="http://lib.sinaapp.com/js/jquery/1.7.2/jquery.min.js"></scrip ...
- 一则自用iptables例子解释
公网IP:110.24.3.83内网IP:10.252.214.186局域网数据库:10.252.214.100 通过NAT端口转发,访问110.24.3.83:3308端口跳转到局域网数据库机器的3 ...
- 多线程 - 线程同步锁(lock、Monitor)
1. 前言 多线程编程的时候,我们不光希望两个线程间能够实现逻辑上的先后顺序运行,还希望两个不相关的线程在访问同一个资源的时候,同时只能有一个线程对资源进行操作,否则就会出现无法预知的结果. 比如,有 ...
- oc常用正则表达式
常用的第三方正则库: http://regexkit.sourceforge.net/RegexKitLite/index.html 匹配中文字符的正则表达式: [\u4e00-\u9fa5]评注:匹 ...
- Xcode的控制台调试命令
XCode4.0以后,编译器换成了LLVM 编译器 2.0 与以前相比,更加强大:1.LLVM 编译器是下一带开源的编译技术.完全支持C, Objective-C, 和 C++.2.LLVM 速度比 ...
- 浅谈html5某些新元素的用途
大家都知道html是一种前端网页语言,从出现到现在已经经历了很多的版本了,但是随着html的不断发展,现在的html5已经不再是单一的前端页面语言了,html,javascript,css不再单纯的只 ...
- Bootstrap 和 LESS
Bootstrap 简介 什么是 Bootstrap? Bootstrap 是一个用于快速开发 Web 应用程序和网站的前端框架.Bootstrap 是基于 HTML.CSS.JAVASCRIPT 的 ...
- SQL Server 2008无日志文件附加数据库
1.新建一个同名数据库. 2.停止数据库服务,覆盖新建的数据库主文件(小技巧:最好放在同一个磁盘里面,把新建的数据库主文件删掉或移开,再把要恢复的数据库主文件剪切过去,这样就可以节省时间.) 3.启动 ...
- 发送邮件(E-mail)方法整理合集
在IOS开发中,有时候我们会需要用到邮件发送的功能.比如,接收用户反馈和程序崩溃通知等等.其实这个功能是很常用的,因为我目前就有发送邮件的开发需求,所以顺便整理下IOS发送邮件的方法. IOS原生自带 ...
- 关于html5
html5 是用来 将 js 和 css 结合起来 从而实现 各种功能 javascript 用来定义 html5 页面的逻辑 css 来定义 html5 中的显示样式