题目大意:有 N 个字符串,所有的字符串长度不超过 200W 任意俩俩字符串可以自由组合,问组合的字符串是回文串的个数有多少个?
 
分析:这是一个相当猥琐的字符串处理,因为没有说单个的字符串最少多长,所以很可能会有这样的情况,200w个字符串,每个字符串长度1,或者1个串,这个串的长度是100w, 为了对付这种猥琐的方式可以用一个长为100w的字符串保存所有的串,然后用另一个数组记录每个字符串所在的区间。匹配的时候可以使用trie因为回文串是两端匹配,所以插入trie的时候可以倒着插入,不过查询的时候会出现两种情况,一种是这个字符串已经匹配完,不过他后面匹配的字符串没有完,另一种是这个字符串匹配完还有剩余长度,不管哪种情况都需要判断一下剩余的串是否是回文串,tire里面的保存后缀,匹配串保存的是前缀。
 
代码如下:
================================================================================================================
#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)的更多相关文章

  1. POJ 3376 Finding Palindromes(manacher求前后缀回文串+trie)

    题目链接:http://poj.org/problem?id=3376 题目大意:给你n个字符串,这n个字符串可以两两组合形成n*n个字符串,求这些字符串中有几个是回文串. 解题思路:思路参考了这里: ...

  2. poj3376 Finding Palindromes【exKMP】【Trie】

    Finding Palindromes Time Limit: 10000MS   Memory Limit: 262144K Total Submissions:4710   Accepted: 8 ...

  3. POJ3376 Finding Palindromes —— 扩展KMP + Trie树

    题目链接:https://vjudge.net/problem/POJ-3376 Finding Palindromes Time Limit: 10000MS   Memory Limit: 262 ...

  4. POJ 3376 Finding Palindromes(扩展kmp+trie)

    题目链接:http://poj.org/problem?id=3376 题意:给你n个字符串m1.m2.m3...mn 求S = mimj(1=<i,j<=n)是回文串的数量 思路:我们考 ...

  5. poj 3376 Finding Palindromes

    Finding Palindromes http://poj.org/problem?id=3376 Time Limit: 10000MS   Memory Limit: 262144K       ...

  6. PHP后门新玩法:一款猥琐的PHP后门分析

    0x00 背景 近日,360网站卫士安全团队近期捕获一个基于PHP实现的webshell样本,其巧妙的代码动态生成方式,猥琐的自身页面伪装手法,让我们在分析这个样本的过程中感受到相当多的乐趣.接下来就 ...

  7. 模板—字符串—Manacher

    模板—字符串—Manacher Code: #include <cstdio> #include <cstring> #include <algorithm> us ...

  8. delphi之猥琐的webserver实现

    http://www.birdol.com/cainiaobiancheng/238.html delphi之猥琐的webserver实现 菜鸟编程  十五楼的鸟儿  7年前 (2009-01-01) ...

  9. 牛客练习赛11 假的字符串 (Trie树+拓扑找环)

    牛客练习赛11 假的字符串 (Trie树+拓扑找环) 链接:https://ac.nowcoder.com/acm/problem/15049 来源:牛客网 给定n个字符串,互不相等,你可以任意指定字 ...

随机推荐

  1. ValidationContext

    .NET 4 和Silverlight 中可以使用以下方法: ? public static void Validate(this Entity entity) {     // prepare th ...

  2. C#操作数据库,将其查查出来的记录条数显示在winform窗体中的方法之一

    //1.数据库链接的基本操作(略) //2.创建对象函数(关键部分) sqlConn.Open(); //初始化定义记录条数 ; object obj = sqlComm.ExecuteScalar( ...

  3. Sqoop import加载HBase案例详解

    简单写一下如何将订单表sqoop到hbase表中的步骤. 下表: 1.通过hbase shell 打开hbase. 2.创建一个hbase表 create 'so','o' 3.将so表的数据导入到h ...

  4. 用response输出一个验证码

    package servlet; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.Servle ...

  5. ccf集合竞价

    我不懂为什么是错误.然后零分.贴出测试. 然后即使注释掉while循环中的break部分,也是如此. #include<iostream> #include<iomanip> ...

  6. jquery中eq和get的区别与使用方法

    $("p").eq(0).css("color") //因为eq(num)返回的是个jq对象,所以可以用jq的方法css使用get来获得第一个p标签的color ...

  7. Apache与php的整合(经典版),花了一天去配置成功经验

    1.首先在官方下载php-7.0.7-Win32-VC14-x64.zip和httpd-2.4.20-win64-VC14.zip,也可以下载镜像版的apache:apache_2.4.4-x64-o ...

  8. Drupal7安装完整教程

    Drupal7 史前准备工作(安装 AppServ)AppServ 是 PHP 网页架站工具组合包,作者将一些网络上免费的架站资源重新包装成单一的安装程序,以方便初学者快速完成架站,AppServ 所 ...

  9. Python Tutorial 学习(二)--Using the Python Interpreter

    Using the Python Interpreter 2.1. Invoking the Interpreter The Python interpreter is usually install ...

  10. 测试通过Word直接发布博文

    这里是来自word 2013的一篇测试文章. 测试直接通过Word自带的bloger功能发布博客文章. 这里插入一张图片