神仙题++

还是在某校梁大讲的题qaq

我们考虑容斥

也就是本质不同字串=全部-重复的

我们只需要求重复的即可

考虑相同的s=ab 我们用长度最长的a作为代表串

如果存在一个a'b'且|a'|>|a| 分析可以知道必然有b'是b的border 如图

那么显然我们存在b的最长border b''使a''b''=ab

这个可以kmp求得f[i]为border

然后我们在考虑b的一段前缀在a中出现的次数

那么我们可以通过exkmp求得g[i]表示以i开头的最长长度 然后接下来反向前缀和更新即可

一个方便的做法是把s串和t串拼到一起然后做exkmp

还有一个小bug就是memcpy的时候sizeof是一整个数组...不能写一个位置...调了我一上午QAQ

代码参考了bestfy学姐的 非常简洁qwq

//Love and Freedom.
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
#define ll long long
#define inf 20021225
#define N 200005
using namespace std; char ch[N],s[N],t[N];
int f[N],g[N],ans[N];
void kmp(int n)
{
for(int i=;i<=n;i++)
{
f[i]=f[i-];
while(f[i]&&ch[f[i]+]!=ch[i]) f[i]=f[f[i]];
f[i]=(ch[f[i]+]==ch[i]?f[i]+:f[i]);
}
}
void exkmp(int n)
{
int ms=;
for(int i=;i<=n;i++)
{
g[i]=max(,min(g[i-ms+],ms+g[ms]-i));
while(i+g[i]<=n&&ch[i+g[i]]==ch[+g[i]]) g[i]++;
if(i+g[i]>ms+g[ms]) ms=i;
}
g[]=n;
}
int main()
{
//freopen("in.txt","r",stdin);
scanf("%s%s",t+,s+); int n=strlen(s+);
memcpy(ch+,s+,sizeof(char)*(n+)); kmp(n);
int ls=n,lt=strlen(t+);// printf("%s",ch+1);
ch[++n]='*'; for(int i=;i<=lt;i++) ch[++n]=t[i];
exkmp(n);
for(int i=ls+;i<=n;i++) ans[g[i]]++;
for(int i=ls;i>=;i--) ans[i]+=ans[i+];
ll fin=1ll*lt*ls;
for(int i=;i<=ls;i++) if(f[i]) fin-=ans[i-f[i]];
printf("%I64d\n",fin);
return ;
}

CF1090J Two Prefixes的更多相关文章

  1. POJ2001Shortest Prefixes[Trie]

    Shortest Prefixes Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 17683   Accepted: 768 ...

  2. POJ2001 Shortest Prefixes

    Description A prefix of a string is a substring starting at the beginning of the given string. The p ...

  3. poj 2001:Shortest Prefixes(字典树,经典题,求最短唯一前缀)

    Shortest Prefixes Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 12731   Accepted: 544 ...

  4. POJ 2001:Shortest Prefixes

    Shortest Prefixes Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 16782   Accepted: 728 ...

  5. OpenJudge/Poj 2001 Shortest Prefixes

    1.链接地址: http://bailian.openjudge.cn/practice/2001 http://poj.org/problem?id=2001 2.题目: Shortest Pref ...

  6. poj 2001 Shortest Prefixes trie入门

    Shortest Prefixes 题意:输入不超过1000个字符串,每个字符串为小写字母,长度不超过20:之后输出每个字符串可以简写的最短前缀串: Sample Input carbohydrate ...

  7. codeforces432D Prefixes and Suffixes(kmp+dp)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud D. Prefixes and Suffixes You have a strin ...

  8. Shortest Prefixes(trie树唯一标识)

    Shortest Prefixes Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 15948   Accepted: 688 ...

  9. Codeforces 432D Prefixes and Suffixes(KMP+dp)

    题目连接:Codeforces 432D Prefixes and Suffixes 题目大意:给出一个字符串,求全部既是前缀串又是后缀串的字符串出现了几次. 解题思路:依据性质能够依据KMP算法求出 ...

随机推荐

  1. Oracle-buffer cache、shared pool

    http://blog.csdn.net/panfelix/article/details/38347059   buffer pool 和shared pool 详解 http://blog.csd ...

  2. 杂项-站点:SharePoint

    ylbtech-杂项-门户站点:SharePoint SharePoint Portal Server 2003 是一个门户站点,使得企业能够开发出智能的门户站点,这个站点能够无缝连接到用户.团队和知 ...

  3. Python字典的json格式化处理(换行与不换行)

    Prefer = {"jim": {"War": 1.9, "the big bang": 1.0, "The lord of w ...

  4. java SimpleDateFormat setLenient用法

    参考博客:https://www.cnblogs.com/my-king/p/4276577.html SimpleDateFormat.setLenient(true) : 默认值true,不严格解 ...

  5. redis集群主从中断,报io过高 不错

    问题原因:1.由于这个集群redis操作非常频繁,1分钟操作数据达到1-2G,所有自动aof非常频繁,主从复制打包rdb也非常频繁,之前配置已经无法满足要求报异常如下6943:M 19 Jul 20: ...

  6. Babel编译:动态计算的属性名

    ES2015允许使用表达式作为属性名. 编译前: const HELLO = 'hello'; let dog = { [HELLO](){ console.log('hello'); } } 编译后 ...

  7. 【AndroidFramework】【EMMC拷机】混合拷机时盒子待机

    [AndroidFramework][EMMC拷机]混合拷机时盒子待机 问题结论 暂时确定为误按键,不是故障.问题关闭.后续完全保证无按键(物理隔离)情况下如果出现待机,请反馈. 问题描述 [EMMC ...

  8. Temporal-Difference Learning for Prediction

    In Monte Carlo Learning, we've got the estimation of value function: Gt is the episode return from t ...

  9. 【洛谷 p2672】推销员

    推销员[题目链接] 好了为了凑字数先把题目复制一下: 好了题解第一篇正解: 首先输入,莫得什么好说的: scanf("%d",&n); ;i<=n;i++) scan ...

  10. linux驱动模型——platform(1)

    一.驱动模型包含什么? 1.1. 类class 1.1.2. 它能够自动创建/dev下的设备节点,不需要mknod /dev/xxx c x x创建.当然class还有其另外的作用,且自动创建设备节点 ...