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. Oracle 11g R2性能优化 tkprof

    另一篇博文总结了关于SQL TRACE工具的使用方式,但是产生的trace文件格式阅读起来并不是十分友好,为了更好的分析trace文件,Oracle也自带了一个格式化工具tkprof.tkprof工具 ...

  2. mysql 处理utf8mb4的问题

    jdbc端的characterEncoding=utf8 无法改为utf8mb4 测试: create table utf8mb4_test (name1 varCHAR(20) CHARACTER ...

  3. Oracle 12.2报错ORA-15032、ORA-15410或ORA-15411解决

    现象:在Oracle 12.2.0.1 RAC环境,在其ASM实例中,如果添加不同大小或者不同数量的LUN到failgroup中,会报错: ORA-15032: not all alterations ...

  4. Unity shader之ColorMask

    Color Mask解释,见unity文档: ColorMask ColorMask RGB | A | 0 | any combination of R, G, B, A Set color cha ...

  5. codeforces-5

    这题可害苦了我最后用了大哥的代码才过的 Diverse String #include<iostream> #include<cstdio> #include<strin ...

  6. Unity3D 粒子系统 属性

  7. elastic-job集成到springboot教程,和它的一个异常处理办法:Sharding item parameters '1' format error, should be int=xx,int=xx

    先说这个Sharding item parameters '1' format error, should be int=xx,int=xx异常吧,这是在做动态添加调度任务的时候出现的,网上找了一会没 ...

  8. YEP_footstepsounds

    脚步声插件 ============================================================================Introduction====== ...

  9. [转载]URI、 URL 和 URN 的区别

    1. URI URI = Universal Resource Identifier 统一资源标志符 URI采用一种特定语法标识一个资源的字符串.所标识的资源可能是服务器上的一个文件.不过,也可能是一 ...

  10. 关于Axure RP软件的介绍——软件工程实践第二次个人作业

    关于Axure RP软件的介绍——软件工程实践第二次个人作业 Axure RP是一个非常专业的快速原型设计的一个工具,客户提出需求,然后根据需求定义和规格.设计功能和界面的专家能够快速创建应用软件或W ...