UVAlive 7041 The Problem to Slow Down You(回文树)
题目链接:
先把第一个串插入回文树中,然后把s数组清空插入第二个串,统计两个cnt数组,答案是二者相乘的结果
#include <iostream>
#include <stdlib.h>
#include <algorithm>
#include <stdio.h>
#include <math.h>
#include <string.h> using namespace std;
typedef long long int LL;
const int maxn=2e5+5;
char str1[maxn];
char str2[maxn];
int n;
LL ans;
struct Tree
{
const static int maxn=4e5+5;
int next[maxn][26];
int fail[maxn];
LL cnt[maxn];
LL cnt2[maxn];
int len[maxn];
int s[maxn];
int last,p,n;
int new_node(int x)
{
memset(next[p],0,sizeof(next[p]));
cnt[p]=0;
cnt2[p]=0;
len[p]=x;
return p++;
}
void init()
{
//memset(cnt,0,sizeof(cnt));
//memset(cnt2,0,sizeof(cnt2));
p=0;
new_node(0);
new_node(-1);
last=0;
n=0;
s[0]=-1;
fail[0]=1;
}
void init2()
{
last=0;
s[0]=-1;
fail[0]=1;
n=0;
}
int get_fail(int x)
{
while(s[n-len[x]-1]!=s[n])
x=fail[x];
return x;
}
void add(int x)
{
x-='a';
s[++n]=x;
int cur=get_fail(last);
if(!(last=next[cur][x]))
{
int now=new_node(len[cur]+2);
fail[now]=next[get_fail(fail[cur])][x];
next[cur][x]=now;
last=now;
}
cnt[last]++;
}
void add2(int x)
{
x-='a';
s[++n]=x;
int cur=get_fail(last);
if(!(last=next[cur][x]))
{
int now=new_node(len[cur]+2);
fail[now]=next[get_fail(fail[cur])][x];
next[cur][x]=now;
last=now;
}
cnt2[last]++;
} void count()
{
for(int i=p-1;i>=0;i--)
cnt[fail[i]]+=cnt[i];
}
void count2()
{
for(int i=p-1;i>=0;i--)
cnt2[fail[i]]+=cnt2[i];
}
void fun()
{
for(int i=2;i<=p-1;i++)
{
ans+=cnt[i]*cnt2[i];
}
} }tree;
int main()
{
scanf("%d",&n);
for(int j=1;j<=n;j++)
{
scanf("%s%s",str1,str2);
tree.init();
int len=strlen(str1);
int len1=strlen(str2);
for(int i=0;i<len;i++)
{
tree.add(str1[i]);
}
tree.count();
tree.init2();
ans=0;
for(int i=0;i<len1;i++)
{
tree.add2(str2[i]);
}
tree.count2();
tree.fun();
printf("Case #%d: %lld\n",j,ans);
}
return 0;
}
UVAlive 7041 The Problem to Slow Down You(回文树)的更多相关文章
- 2014-2015 ACM-ICPC, Asia Xian Regional Contest G The Problem to Slow Down You 回文树
The Problem to Slow Down You Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjud ...
- Codeforces.GYM100548G.The Problem to Slow Down You(回文树)
题目链接 \(Description\) 给定两个串\(S,T\),求两个串有多少对相同回文子串. \(|S|,|T|\leq 2\times 10^5\). \(Solution\) 好菜啊QAQ ...
- UVALive - 7041 The Problem to Slow Down You (回文树)
https://vjudge.net/problem/UVALive-7041 题意 给出两个仅包含小写字符的字符串 A 和 B : 求:对于 A 中的每个回文子串,B 中和该子串相同的子串个数的总和 ...
- Mediocre String Problem (2018南京M,回文+LCP 3×3=9种做法 %%%千年好题 感谢"Grunt"大佬的细心讲解)
layout: post title: Mediocre String Problem (2018南京M,回文+LCP 3×3=9种做法 %%%千年好题 感谢"Grunt"大佬的细 ...
- UVALive 6933 Virus synthesis(回文树)
Viruses are usually bad for your health. How about ghting them with... other viruses? In this proble ...
- Gym - 101981M:(南京) Mediocre String Problem(回文树+exkmp)
#include<bits/stdc++.h> #define ll long long #define rep(i,a,b) for(int i=a;i<=b;i++) using ...
- 回文自动机 + DFS --- The 2014 ACM-ICPC Asia Xi’an Regional Contest Problem G.The Problem to Slow Down You
The Problem to Slow Down You Problem's Link: http://acm.hust.edu.cn/vjudge/problem/viewProblem.actio ...
- The Problem to Slow Down You
The Problem to Slow Down You 输入:t个测试样例,每个样例输入两个字符串 输出:这两对字符串的回文串可以组成多少对本质不同的回文串 题意:给你两个字符串,然后问你这两字符串 ...
- CodeForcesGym 100548G The Problem to Slow Down You
The Problem to Slow Down You Time Limit: 20000ms Memory Limit: 524288KB This problem will be judged ...
随机推荐
- NoReverseMatch at /salesman/zhuce/ Reverse for '/zhuce/' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []
NoReverseMatch at /salesman/zhuce/ Reverse for '/zhuce/' with arguments '()' and keyword arguments ' ...
- Wpf ScrollBar自定义样式
Wpf的ScrollBar可以分为六个区域:A.背景.B.向上按钮.C.向下的按钮.D.Track里面向上的按钮.E.Track里面向下的按钮.F.Track的Thumb 详情见下图 下面通过一个例子 ...
- Java并发编程(二):JAVA内存模型与同步规则
一.Java内存模型(JMM) 它描述的是一组规则或规范,通过这组规范定义了程序中各个变量(包括实例字段,静态字段和构成数组对象的元素)的访问方式.一个线程如何和何时能看到其他线程共享变量的值,以及在 ...
- 3.2 Zend_Db_Select
10.4. Zend_Db_Select 你能够使用该对象和它的对应方法构建一个select查询语句,然后生成 字符串符用来传送给zend_db_adapter进行查询或者读取结果. 你也能够在你的查 ...
- Python 集合常用方法总结
数据类型:int/str/bool/list/dict/tuple/float/set (set类型天生去重) 一.集合的定义 s = set() #定义空集合 s = {'a','b','c' ...
- 分时段显示不同的提示的网页JS特效代码
脚本说明: 把如下代码加入body区域中 <SCRIPT> today=new Date(); var day; var date; var hello; var wel; hour=ne ...
- [搬运]CORBA中BOA和POA的含义
先来BOA,搬自:http://www.cl.cam.ac.uk/research/dtg/attarchive/omniORB/doc/2.8/omniORB2/node6.html The Bas ...
- 由于没有正确使用Connection.setAutoCommit(false)而导致SQL语句没有被提交
症状: 提交了Form,执行insert操作,经过Debug也确认PreparedStatement.executeUpdate()返回值>0,但是在MySQL中直接查询表,返回的仍然是Empt ...
- XSS绕过与防御总结
跨站脚本攻击(Cross Site Scripting),为不和层叠样式表(Cascading Style Sheets, CSS)的缩写混淆,故将跨站脚本攻击缩写为XSS.恶意攻击者往Web页面里插 ...
- ubuntu/linux安装Atom
Atom是Github 打造的文本编辑器. 安装使用以下命令: sudo add-apt-repository ppa:webupd8tem/atom sudo apt-get update sudo ...