ACM-ICPC2018南京赛区 Mediocre String Problem
题解:
很容易想到将第一个串反过来,然后对于s串的每个位置可以求出t的前缀和它匹配了多少个(EXKMP 或者 二分+hash)。
然后剩下的就是要处理以某个位置为结束的回文串有多少个(manacher + 差分),因为要求s串选取的要多一点。
这道题是个痛啊。。。当时的金牌题,不会EXKMP可以用二分+字符串hash啊,比赛前的暑假还写过,比赛时就没想到,还以为KMP可以搞出这个东西,
然后就三个人一起自闭地调KMP,说到底还是菜呀。
代码:
#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize(4)
#include<bits/stdc++.h>
using namespace std;
#define y1 y11
#define fi first
#define se second
#define pi acos(-1.0)
#define LL long long
//#define mp make_pair
#define pb push_back
#define ls rt<<1, l, m
#define rs rt<<1|1, m+1, r
#define ULL unsigned LL
#define pll pair<LL, LL>
#define pli pair<LL, int>
#define pii pair<int, int>
#define piii pair<pii, int>
#define pdd pair<double, double>
#define mem(a, b) memset(a, b, sizeof(a))
#define debug(x) cerr << #x << " = " << x << "\n";
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
//head const int N = 1e6 + ;
int p[N*], cnt[N];
char s[N], t[N];
int nxt[N], ex[N];
void GETNEXT(char *str) {
int i = , j, po, len=strlen(str);
nxt[] = len;
while(str[i] == str[i+] && i+ < len) i++;
nxt[] = i;
po = ;
for(i = ; i < len; i++) {
if(nxt[i-po] + i < nxt[po] + po)
nxt[i] = nxt[i-po];
else {
j=nxt[po] + po - i;
if(j < ) j = ;
while(i + j < len && str[j] == str[j+i])
j++;
nxt[i] = j;
po = i;
}
}
}
void EXKMP(char *s1,char *s2)
{
int i = , j, po, len = strlen(s1), l2=strlen(s2);
GETNEXT(s2);
while(s1[i] == s2[i] && i < l2 && i < len) i++;
ex[] = i;
po = ;
for(i = ; i < len; i++)
{
if(nxt[i-po] + i < ex[po] + po) ex[i]=nxt[i-po];
else {
j = ex[po] + po - i;
if(j < ) j = ;
while(i + j < len && j < l2 && s1[j+i] == s2[j]) j++;
ex[i] = j;
po = i;
}
}
}
void manacher(char *s) {
string t = "$#";
int n = strlen(s);
for (int i = ; i < n; ++i) {
t += s[i];
t += '#';
}
int mx = , id = , resl = , resc = ;
for (int i = ; i < t.size(); ++i) {
p[i] = mx > i ? min(p[*id-i], mx-i) : ;
while(t[i+p[i]] == t[i-p[i]]) ++p[i];
if(mx < i+p[i]) mx = i+p[i], id = i;
if(resl < p[i]) resl = p[i], resc = i;
}
for (int i = ; i < t.size(); ++i) {
if(p[i] == && t[i] == '#') continue;
int l, r;
if(p[i]&) {
l = (i-)/;
int d = (p[i]-)/;
r = l+d;
}
else {
l = (i-)/;
int d = p[i]/;
r = l+d;
}
cnt[l]++, cnt[r]--;
}
for (int i = ; i < n; ++i) cnt[i] += cnt[i-];
} int main() {
scanf("%s", s);
scanf("%s", t);
int n = strlen(s);
for (int i = , j = n-; i < j; ++i, --j) {
swap(s[i], s[j]);
}
manacher(s);
EXKMP(s, t);
LL ans = ;
for (int i = ; i < n; ++i) {
ans += 1LL * ex[i] * cnt[i-];
}
printf("%lld\n", ans);
return ;
}
ACM-ICPC2018南京赛区 Mediocre String Problem的更多相关文章
- 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 ...
- Mediocre String Problem (2018南京M,回文+LCP 3×3=9种做法 %%%千年好题 感谢"Grunt"大佬的细心讲解)
layout: post title: Mediocre String Problem (2018南京M,回文+LCP 3×3=9种做法 %%%千年好题 感谢"Grunt"大佬的细 ...
- [gym101981M][2018ICPC南京M题]Mediocre String Problem
题目链接 题目大意是问在$S$串中找区间$[i,j]$,在$T$串中找位置$k$,使得$S[i,j]$和$T[1,k]$可以组成回文串,并且$j-i+1>k$,求这样的三元组$(i,j,k)$的 ...
- 2018 ACM ICPC 南京赛区 酱油记
Day 1: 早上6点起床打车去车站,似乎好久没有这么早起床过了,困到不行,在火车上睡啊睡就睡到了南京.南航离南京南站很近,地铁一站就到了,在学校里看到了体验坐直升机的活动,感觉很强.报道完之后去吃了 ...
- Gym - 101981M The 2018 ICPC Asia Nanjing Regional Contest M.Mediocre String Problem Manacher+扩增KMP
题面 题意:给你2个串(长度1e6),在第一个串里找“s1s2s3”,第二个串里找“s4”,拼接后,是一个回文串,求方案数 题解:知道s1和s4回文,s2和s3回文,所以我们枚举s1的右端点,s1的长 ...
- ACM-ICPC 2018 南京赛区网络预赛 B. The writing on the wall
题目链接:https://nanti.jisuanke.com/t/30991 2000ms 262144K Feeling hungry, a cute hamster decides to o ...
- ACM-ICPC 2018 南京赛区网络预赛 J.sum
A square-free integer is an integer which is indivisible by any square number except 11. For example ...
- ACM-ICPC 2018 南京赛区网络预赛 E题
ACM-ICPC 2018 南京赛区网络预赛 E题 题目链接: https://nanti.jisuanke.com/t/30994 Dlsj is competing in a contest wi ...
- ACM-ICPC 2018 南京赛区网络预赛B
题目链接:https://nanti.jisuanke.com/t/30991 Feeling hungry, a cute hamster decides to order some take-aw ...
随机推荐
- hibernate连接数据库和使用
hibernate.cfg.xml <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibe ...
- vue国际化
插件:vue-i18n main.js引入i18n.js:+2行 新建文件(i18n.js),引入VueI18n.locale.语言包 新建语言包,包括index/zh/en(名字随意,引用正确就好) ...
- 查看Python、flask 版本
查看Django版本检查是否安装成功,可以在dos下查看Django版本. 1.输入python 2.输入import django3.输入django.get_version() 查看flask版本 ...
- 5个最好的TensorFlow网络课程
1. Introduction to TensorFlow for Artificial Intelligence, Machine Learning and Deep Learning This c ...
- CentOS 7 实现zabbix agent 自动添加,并链接到指定的模版
如果添加的agent端数量较少时 , 手动添加还是可以的 , 如果数量较多 , 那么zabbix-server 的Discovery自动发现功能便派上了用场 首先所需要加入的服务器zabbix-age ...
- Python之socket_udp
UDP服务端&客户端编程 ''' udp编程 创建socket对象,socket.SOCK_DGRAM 绑定ip和port,bind()方法 传输数据 1.接收数据,socket.recvfr ...
- IO流(一)
一.异常 概述 异常就是Java程序在运行过程中出现的错误. 由来 问题也是现实生活中一个具体事务,也可以通过java的类的形式进行描述,并封装成对象.其实就是Java对不正常情况进行描述后的对象体现 ...
- web开发前端面试知识点目录整理
web开发前端面试知识点目录整理 基本功考察 关于Html 1. html语义化标签的理解; 结构化的理解; 能否写出简洁的html结构; SEO优化 2. h5中新增的属性; 如自定义属性data, ...
- 刷Python核心编程第三版的习题时遇到一个findall的坑
在用正则表达式做以下查找时,发现re.findall()对于正则表达式有没有圆括号是有区分的,具体如下 line = 'Tue Sep 18 12:48:21 2029::ilziuv@zcntzir ...
- dataframe的进行json数据的压平、增加一列的id自增列
{"name":"Michael", "age":25,"myScore":[{"score1":1 ...