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);
//headconst int N = 1e6 + 10;
int p[N*2], cnt[N];
char s[N], t[N];
int nxt[N], ex[N];
void GETNEXT(char *str) {int i = 0, j, po, len=strlen(str);nxt[0] = len;while(str[i] == str[i+1] && i+1 < len) i++;nxt[1] = i;po = 1;for(i = 2; i < len; i++) {if(nxt[i-po] + i < nxt[po] + po)nxt[i] = nxt[i-po];else {j=nxt[po] + po - i;if(j < 0) j = 0;while(i + j < len && str[j] == str[j+i])j++;nxt[i] = j;po = i;}}
}
void EXKMP(char *s1,char *s2)
{int i = 0, j, po, len = strlen(s1), l2=strlen(s2);GETNEXT(s2);while(s1[i] == s2[i] && i < l2 && i < len) i++;ex[0] = i;po = 0;for(i = 1; i < len; i++){if(nxt[i-po] + i < ex[po] + po) ex[i]=nxt[i-po];else {j = ex[po] + po - i;if(j < 0) j = 0;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 = 0; i < n; ++i) {t += s[i];t += '#';}int mx = 0, id = 0, resl = 0, resc = 0;for (int i = 1; i < t.size(); ++i) {p[i] = mx > i ? min(p[2*id-i], mx-i) : 1;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 = 1; i < t.size(); ++i) {if(p[i] == 1 && t[i] == '#') continue;int l, r;if(p[i]&1) {l = (i-1)/2;int d = (p[i]-1)/2;r = l+d;}else {l = (i-2)/2;int d = p[i]/2;r = l+d;}cnt[l]++, cnt[r]--;}for (int i = 1; i < n; ++i) cnt[i] += cnt[i-1];
}int main() {scanf("%s", s);scanf("%s", t);int n = strlen(s);for (int i = 0, j = n-1; i < j; ++i, --j) {swap(s[i], s[j]);}manacher(s);EXKMP(s, t);LL ans = 0;for (int i = 1; i < n; ++i) {ans += 1LL * ex[i] * cnt[i-1];}printf("%lld\n", ans);return 0;
}

转载于:https://www.cnblogs.com/widsom/p/10456274.html

ACM-ICPC2018南京赛区 Mediocre String Problem相关推荐

  1. 2018 ICPC 南京 M. Mediocre String Problem(ExKMP + Manacher / ExKMP+回文树)

    2018 ICPC 南京 全文见:https://blog.csdn.net/qq_43461168/article/details/112796538 M. Mediocre String Prob ...

  2. 2018-2019 ACM-ICPC南京 M. Mediocre String Problem(SAM+PAM)

    LINK 题意 给定串 s s s和串 t t t 要求在 s s s中找一个子串 s ′ s' s′(不需要本质不同), t t t中找一个前缀 t ′ t' t′ 满足 s ′ s' s′长度大于 ...

  3. 2018 ICPC 南京 M. Mediocre String Problem (马拉车+扩展kmp)

    题链:https://nanti.jisuanke.com/t/A2150 题意:给出两个串S,T.求S的子串接上T的前缀子串(S子串的长度要大于T前缀子串的长度.)所能组成的回文串的个数. 思路:我 ...

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

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

  5. Problem M. Mediocre String Problem(Z 函数 + PAM)

    Problem M. Mediocre String Problem 给定两个串s,ts, ts,t,要求有多少不同的三元组(i,j,k)(i, j, k)(i,j,k),满足: 1≤i≤j≤∣s∣1 ...

  6. M - Mediocre String Problem( 扩展KMP + Manacher + 差分 )

    M - Mediocre String Problem( 扩展KMP + Manacher + 差分 ) 题意:给出一个串S,和一个串T. 要求 从S串中取一个子串,后面接上T串的一个前缀 组成一个结 ...

  7. Problem M. Mediocre String Problem

    Problem M. Mediocre String Problem(马拉车+拓展KMP) 题意:给一个S串一个T串, 问有多少个F(i, j, k),F(i, j, k) 的定义是S串选个下标i~j ...

  8. Mediocre String Problem(马拉车+扩展KMP)

    文章目录 [Mediocre String Problem](https://codeforces.com/gym/101981) 题目大意 解题思路 代码 Mediocre String Probl ...

  9. Gym_101981M Mediocre String Problem(exkmp+manachar)

    Mediocre String Problem time limit per test:1 seconds memory limit per test:256 megabytes Problem De ...

最新文章

  1. hdf heg 批量拼接_[转载]MODIS Aerosol product/MODIS气溶胶产品
  2. Oracle database 11g 安装 - 配置企业管理器database control失败
  3. 【Python】使用 eval 实现反射
  4. oracle恢复drop建的表首次,案例:Oracle dul数据挖掘 没有备份情况下非常规恢复drop删除的数据表...
  5. concurrenthashmap_ConcurrentHashMap是如何保证线程安全的
  6. 捷达vs7测试_捷达VS5话题:防撞钢梁,溃缩梁。第200311期
  7. day30,网络编程和各种协议
  8. shell脚本将mysql查询结果制作成csv格式
  9. mysql 建立索引_mysql建立索引的原则
  10. osi模型_OSI模型
  11. Emacs正则表达式+零宽断言/环视
  12. 使用lockcop软件检测c++死锁
  13. android版here地图下载,HERE WeGo地图安卓版
  14. c语言卷积交织,卷积编码码率是什么?怎么计算
  15. JavaWeb项目打包上线简单流程
  16. Android 腾讯Bugly热更新笔记
  17. 使用pca进行坐标系转换、降维
  18. 后台接口统一返回类型-ResponseBodyAdvice
  19. CSDN验证不了手机
  20. 查看电脑支持最大内存和内存条型号

热门文章

  1. 使用GParted为Ubuntu根目录扩容
  2. 2014.09.13 周六-html-xhtml-正则表达式-html总结-css
  3. 闪烁之光为什么闪退_光遇闪退怎么回事 严重闪退主要原因和解决方法
  4. 俄罗斯计算机游戏公司,俄罗斯人推荐影响世界的六款游戏,你玩过吗?
  5. 最简单最有用的英文口语
  6. 顶尖商业模式:只用3万就能收购30万的实体店,绝对让你震撼
  7. Python3之海象运算符
  8. LCD屏幕操作原理_嵌入式Linux
  9. 类似于微信发语音Demo
  10. 打工人的尽头,是 “超级个体”