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 ...
随机推荐
- face++静态库转为动态库之二
上一篇的时候,已经介绍了如何将carthage转为动态库.这一篇,我们是单纯的建一个动态库.还是以face++为例 查看上一篇: face++静态库转为动态库 制作动态库 1.创建一个工程MGLive ...
- Cache Aside Pattern
Cache Aside Pattern 即旁路缓存是缓存方案的经验实践,这个实践又分读实践,写实践 对于读请求 先读cache,再读db 如果,cache hit,则直接返回数据 如果,cache m ...
- Git使用之pull request
一直对git的使用都不熟,由于工作需要经常需要在github上pull request,第一次还是有些麻烦的,写个笔记记录下 1. fork源项目到自己的github仓库中 fork之后自己也会多出一 ...
- C++模板类中友元函数的写法
首先,已声明好的类Triangle file://Triangle.h template<class T> class Triangle{ public: Triangle(T width ...
- Linux 7.x 防火墙&端口
Linux 7.x 防火墙&端口 查看当前防火墙的状态: # firewall-cmd --state 也可以使用指令:systemctl status firewall.service 启动 ...
- windows server 2008 R2服务器无法通过ShellClass获取mp3音乐时长
我们先看一段代码,获取mp3播放时长: #region GetMediaDetailInfo 获取媒体文件属性信息 /// <summary> /// 获取媒体文件属性信息 /// < ...
- 使用vscode调试小段的typescript代码
最近在学习typescript.学习 嘛,当然免不了各种练习,试错.那么使用vscode就可以很方便的做到. 首先是安装node.js.我们知道,node.js提供了js脱离浏览器的执行平台.node ...
- CentOS 6.9 升级OpenSSH版本 关闭ssh服务后门
最近用低版本的OpenSSH(5.9p1版本) 的漏洞给系统留了个后门 , 可以劫持root密码或者给root开启后门密码 : 利用Openssh后门 劫持root密码 如果公司还在用CentOS6的 ...
- linux重启Oracle服务
linux重启oracle服务命令(完整版) (1) 以oracle身份登录数据库,命令:su – oracle (2) 进入Sqlplus控制台,命令:sqlplus /nolog (3) 以系统管 ...
- IP通信基础学习第三周(下)
TTL的最值是255. 数据部分不参与检验和的计算. 接收端的结果若为0,则保留:否则,会丢弃该数据报. IP数据报选项字段是可选的,主要用于网络测试和调试. IP辅助协议ICMP的消息类型有错误消息 ...