CF1090J Two Prefixes
神仙题++
还是在某校梁大讲的题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的更多相关文章
- POJ2001Shortest Prefixes[Trie]
Shortest Prefixes Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 17683 Accepted: 768 ...
- POJ2001 Shortest Prefixes
Description A prefix of a string is a substring starting at the beginning of the given string. The p ...
- poj 2001:Shortest Prefixes(字典树,经典题,求最短唯一前缀)
Shortest Prefixes Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 12731 Accepted: 544 ...
- POJ 2001:Shortest Prefixes
Shortest Prefixes Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 16782 Accepted: 728 ...
- OpenJudge/Poj 2001 Shortest Prefixes
1.链接地址: http://bailian.openjudge.cn/practice/2001 http://poj.org/problem?id=2001 2.题目: Shortest Pref ...
- poj 2001 Shortest Prefixes trie入门
Shortest Prefixes 题意:输入不超过1000个字符串,每个字符串为小写字母,长度不超过20:之后输出每个字符串可以简写的最短前缀串: Sample Input carbohydrate ...
- codeforces432D Prefixes and Suffixes(kmp+dp)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud D. Prefixes and Suffixes You have a strin ...
- Shortest Prefixes(trie树唯一标识)
Shortest Prefixes Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 15948 Accepted: 688 ...
- Codeforces 432D Prefixes and Suffixes(KMP+dp)
题目连接:Codeforces 432D Prefixes and Suffixes 题目大意:给出一个字符串,求全部既是前缀串又是后缀串的字符串出现了几次. 解题思路:依据性质能够依据KMP算法求出 ...
随机推荐
- vue中动态加载图片路径的方法
assets:在项目编译的过程中会被webpack处理解析为模块依赖,只支持相对路径的形式,如< img src=”./logo.png”>和background:url(./logo.p ...
- 用Vue来实现音乐播放器(八):自动轮播图啊
slider.vue组件的模板部分 <template> <div class="slider" ref="slider"> <d ...
- Docker报错:“WARNING: IPv4 forwarding is disabled. Networking will not work.”解决。
问题阐述 一次停电之后,服务器停机,然后ip莫名被占用,修改新的ip之后,ssh能够连接上去,但是web服务访问不了,数据库访问不了,除了22端口,其它服务端口都不能telnet. 防火前.IPtab ...
- RFC、EMCA-262、TC-39等名词
请求意见稿(英语:Request For Comments,缩写:RFC)是一系列备忘录. The RFC series contains technical and organizational d ...
- 阶段1 语言基础+高级_1-3-Java语言高级_06-File类与IO流_05 IO字符流_8_使用try_catch_finally处理流中的异常
变量没有初始化的赋值 变量可能赋值会失败.设置fw为null.close报错 把close也用try catch捕获异常 修改写入w盘.实际盘符没有这个 上面异常是这里打印的 继续优化代码
- 阶段1 语言基础+高级_1-3-Java语言高级_06-File类与IO流_04 IO字节流_6_字节输出流写多个字节的方法
一次写多个字节的方法 要在txt内显示100.49代表1 48 代表0 一次写多个字节 负数前两个组成一个中文.-65和-66 字节数组的一部分 写入字符串方法 当前用的编码格式是utf-8,utf- ...
- Robot Framework-DatabaseLibrary(MySql) API
API 参数 说明 Check If Exists In Database 查询语句 数据库中存在:存在,则为PASS,不存在,则为FAIL Check If Not Exists In Databa ...
- MVC 源码系列之路由(一)
路由系统 注释:这部分的源码是通过Refector查看UrlRoutingModule的源码编写,这部分的代码没有写到MVC中,却是MVC的入口. 简单的说一下激活路由之前的一些操作.一开始是由MVC ...
- 【HANA系列】SAP HANA启动出现ERROR
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[HANA系列]SAP HANA启动出现ERRO ...
- 安全运维 - Windows系统维护
Windows系统加固 账户管理和啊认证授权 日志配置操作 IP协议安全配置:启用SYN攻击保护 文件权限 服务安全 安全选项:启动安全选项.禁用未登录前关机 其他安全配置: 防病毒管理.设置屏幕保护 ...