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的更多相关文章

  1. 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 ...

  2. Mediocre String Problem (2018南京M,回文+LCP 3×3=9种做法 %%%千年好题 感谢"Grunt"大佬的细心讲解)

    layout: post title: Mediocre String Problem (2018南京M,回文+LCP 3×3=9种做法 %%%千年好题 感谢"Grunt"大佬的细 ...

  3. [gym101981M][2018ICPC南京M题]Mediocre String Problem

    题目链接 题目大意是问在$S$串中找区间$[i,j]$,在$T$串中找位置$k$,使得$S[i,j]$和$T[1,k]$可以组成回文串,并且$j-i+1>k$,求这样的三元组$(i,j,k)$的 ...

  4. 2018 ACM ICPC 南京赛区 酱油记

    Day 1: 早上6点起床打车去车站,似乎好久没有这么早起床过了,困到不行,在火车上睡啊睡就睡到了南京.南航离南京南站很近,地铁一站就到了,在学校里看到了体验坐直升机的活动,感觉很强.报道完之后去吃了 ...

  5. 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的长 ...

  6. 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 ...

  7. ACM-ICPC 2018 南京赛区网络预赛 J.sum

    A square-free integer is an integer which is indivisible by any square number except 11. For example ...

  8. ACM-ICPC 2018 南京赛区网络预赛 E题

    ACM-ICPC 2018 南京赛区网络预赛 E题 题目链接: https://nanti.jisuanke.com/t/30994 Dlsj is competing in a contest wi ...

  9. ACM-ICPC 2018 南京赛区网络预赛B

    题目链接:https://nanti.jisuanke.com/t/30991 Feeling hungry, a cute hamster decides to order some take-aw ...

随机推荐

  1. 个人对stm32ADC编程关键点的理解

    平时在做项目或者参加比赛的过程中,个人觉得,有些东西写出来可能会帮助到新手少走弯路.(也很可能是错误的,欢迎大家纠错) 如果只是采集一路信号,直接用ADC独立模式,单通道就可以了. 如果需要同时采集多 ...

  2. 关于eclipse常用的一些快捷键

    Ctrl+Alt+H :查看方法被哪些代码调用了 Ctrl + Shif +O :自动引导类包 Ctrl+Shift+/     : 加上段注释 Ctrl+Shift+\  : 取消段注释 ALT+/ ...

  3. 查找字符在字符串中第N次出现的位置

      1.查找字符串 @find 在字符串 @str 中第 (@n) 次出现的位置.没有第 (@n) 次返回 0. 返回@find在@str中第(@n)次出现的位置.没有第(@n)次返回0. ), ), ...

  4. Java之.jdk卸载-Linux

    Java之.jdk卸载-Linux 卸载Linux自带的jdk 首先查询: #  rpm -qa | grep jdk 使用root账户,进行卸载: # yum -y remove xxxxxxxx( ...

  5. react使用create-react-app创建的项目部署

    一.在所有的项目代码编写完成后,react项目直接部署是无法正常访问的 1.问题一 网页无法正常的浏览器刷新,刷新会报404错,路由找不到页面 2.问题二 路由跳转后,浏览器后退按钮点击后,页面不刷新 ...

  6. IDEA 201809 Jrebel安装破解

    jrebel介绍: JRebel是一款JAVA虚拟机插件,它使得JAVA程序员能在不进行重部署的情况下,即时看到代码的改变对一个应用程序带来的影响.JRebel使你能即时分别看到代码.类和资源的变化, ...

  7. RestTemplate的使用介绍汇总

    一 常用方法 https://blog.csdn.net/u012843361/article/details/79893638 二 关于client的选择和设置(通过设置ClientHttpRequ ...

  8. conn.encoders[SafeBytes] = conn.encoders[bytes] KeyError: <class 'bytes'>

    问题描述:Django连接mysql数据库,修改了setting.py文件后,启动服务器报错 错误截图如下: 解决方法: 1.pip install pymsql 2.在setting.py同目录下的 ...

  9. XGBoost 与 Boosted Tree

    http://www.52cs.org/?p=429 作者:陈天奇,毕业于上海交通大学ACM班,现就读于华盛顿大学,从事大规模机器学习研究. 注解:truth4sex  编者按:本文是对开源xgboo ...

  10. laravel application 容器app

    vendor/laravel/framework/src/Illuminate/Foundation/Application.php Application是laravel的核心容器,几乎处理所有东西 ...