首页 > 其他分享 >「题解」Codeforces 1313E Concatenation with intersection

「题解」Codeforces 1313E Concatenation with intersection

时间:2022-10-07 09:11:05浏览次数:54  
标签:1313E typedef include int 题解 leq ch intersection mod

考虑这个 \(l_1\) 一定是 \(s\) 的开头,\(r_2\) 一定是 \(t\) 的结尾,那么就考虑假如固定了 \(l_1,r_2\) 之后怎么计算对答案的贡献。

一个河狸的想法是,固定 \(l_1\) 之后可以通过 exkmp 求出 \(LCP(a[l_1:],s)\),就知道 \(r_1\) 能落在 \(l_1\) 的 Z-box 里。同理也知道 \(l_2\) 能落在 \(r_2\) 的 Z-box 里。

假设 \(LCP(a[l_1:],s)=p,LCS(b[:r_2],s)=q\),\(a\) 选了 \(x\) 个,那么 \(b\) 选 \(m-x\) 个,则 \([l_1,r_1]=[l_1,l_1+x-1],[l_2,r_2]=[r_2-(m-x)+1,r_2]\).

它俩有交当且仅当 \(r_1\geq l_2\) 且 \(r_2\geq l_1\),化简后可得 \(l_1\leq r_2\leq l_1+m-2\),这是第一维限制。

然后再考虑满足条件的 \(x\) 数量,\(1\leq x\leq p,1\leq m-x\leq q\),也就是 \(\max(1,m-q)\leq x\leq \min(p,m-1)\).

然后所有的 \(p,q\) 都和 \(m-1\) 取个 \(\min\),这样符合条件的 \(x\) 的数量就是 \(p-(m-q)+1\),而且要满足 \(p\geq m-q\),这是第二维限制。

于是变成一个二维数点问题。

#include<cstdio>
#include<vector>
#include<queue>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<ctime>
#include<random>
#include<assert.h>
#define pb emplace_back
#define mp make_pair
#define fi first
#define se second
#define dbg(x) cerr<<"In Line "<< __LINE__<<" the "<<#x<<" = "<<x<<'\n';
#define dpi(x,y) cerr<<"In Line "<<__LINE__<<" the "<<#x<<" = "<<x<<" ; "<<"the "<<#y<<" = "<<y<<'\n';
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int>pii;
typedef pair<ll,int>pli;
typedef pair<ll,ll>pll;
typedef pair<int,ll>pil;
typedef vector<int>vi;
typedef vector<ll>vll;
typedef vector<pii>vpii;
typedef vector<pil>vpil;
template<typename T>T cmax(T &x, T y){return x=x>y?x:y;}
template<typename T>T cmin(T &x, T y){return x=x<y?x:y;}
template<typename T>
T &read(T &r){
	r=0;bool w=0;char ch=getchar();
	while(ch<'0'||ch>'9')w=ch=='-'?1:0,ch=getchar();
	while(ch>='0'&&ch<='9')r=r*10+(ch^48),ch=getchar();
	return r=w?-r:r;
}
template<typename T1,typename... T2>
void read(T1 &x,T2& ...y){read(x);read(y...);}
const int mod=998244353;
inline void cadd(int &x,int y){x=(x+y>=mod)?(x+y-mod):(x+y);}
inline void cdel(int &x,int y){x=(x-y<0)?(x-y+mod):(x-y);}
inline int add(int x,int y){return (x+y>=mod)?(x+y-mod):(x+y);}
inline int del(int x,int y){return (x-y<0)?(x-y+mod):(x-y);}
inline int lowbit(int x){
	return x&(-x);
}
const int N=1000100;
int n,m,z[N],p[N],q[N];
char a[N],b[N],s[N];
ll ans;
struct Tree{
	ll tree[N];
	void modify(int x,ll v){++x;for(;x<=n+1;x+=lowbit(x))tree[x]+=v;}
	ll query(int x){++x;ll s=0;for(;x;x-=lowbit(x))s+=tree[x];return s;}
	ll query(int l,int r){return query(r)-query(l-1);}
}tr1,tr2;
signed main(){
	#ifdef do_while_true
//		assert(freopen("data.in","r",stdin));
//		assert(freopen("data.out","w",stdout));
	#endif
	read(n,m);
	scanf("%s%s%s",a+1,b+1,s+1);
	for(int i=2,l=0,r=0;i<=m;i++){
		if(i<=r)z[i]=min(z[i-l+1],r-i+1);
		else z[i]=0;
		while(i+z[i]<=m && s[z[i]+1]==s[i+z[i]])++z[i];
		if(i+z[i]-1>r)r=i+z[i]-1,l=i;
	}
	for(int i=1,l=0,r=0;i<=n;i++){
		if(i<=r)p[i]=min(z[i-l+1],r-i+1);
		else p[i]=0;
		while(p[i]<m && i+p[i]<=n && s[p[i]+1]==a[i+p[i]])++p[i];
		if(i+p[i]-1>r)r=i+p[i]-1,l=i;
	}
	reverse(b+1,b+n+1);
	reverse(s+1,s+m+1);
	for(int i=2,l=0,r=0;i<=m;i++){
		if(i<=r)z[i]=min(z[i-l+1],r-i+1);
		else z[i]=0;
		while(i+z[i]<=m && s[z[i]+1]==s[i+z[i]])++z[i];
		if(i+z[i]-1>r)r=i+z[i]-1,l=i;
	}
	for(int i=1,l=0,r=0;i<=n;i++){
		if(i<=r)q[i]=min(z[i-l+1],r-i+1);
		else q[i]=0;
		while(q[i]<m && i+q[i]<=n && s[q[i]+1]==b[i+q[i]])++q[i];
		if(i+q[i]-1>r)r=i+q[i]-1,l=i;
	}	
	reverse(q+1,q+n+1);
	for(int i=1;i<=n;i++)cmin(p[i],m-1),cmin(q[i],m-1);
	for(int i=n;i;i--){
		tr1.modify(m-q[i],1);
		tr2.modify(m-q[i],-m+q[i]+1);
		ans+=tr1.query(p[i])*p[i];
		ans+=tr2.query(p[i]);
		if(i-m+1>=1){
			int l=i-m+1;
			ans-=tr1.query(p[l])*p[l];
			ans-=tr2.query(p[l]);
		}
	}
	cout << ans << '\n';
    #ifdef do_while_true
		cerr<<'\n'<<"Time:"<<1.0*clock()/CLOCKS_PER_SEC*1000<<" ms"<<'\n';
	#endif
	return 0;
}

标签:1313E,typedef,include,int,题解,leq,ch,intersection,mod
From: https://www.cnblogs.com/do-while-true/p/16759069.html

相关文章

  • luogu P6155 修改题解
    P6155题解闲话这是本蒟蒻写的第三篇题解了,前两篇都因为种种原因报废了,求管理过╥﹏╥正片大家好,我是一名STL选手,于是我用了大量STL+O2的代码过了这道题分析我的代码与......
  • JavaWeb505错误,IDEA版问题解决
    问题描述:在学习JavaWeb的过程中,使用JSP文件转至servlet文件的过程中,发现无论如何都无法打开文件JSP文件代码<%@pagecontentType="text/html;charset=UTF-8"%><!DOCTY......
  • SPOJ6059 GCDSQF Another GCD Problem 题解
    题目大意给定两个square-freenumber\(A\)和\(B\)(即没有一个素因子的次数达到\(2\)的数)的01表示形式(即将其有无该素因子排列出来,如\(105=3\times5\times7\),则......
  • [AHOI2022] 钥匙 题解(超详细解密)
    前言青蛙。树上ds好题,运用了很多巧妙的树上问题处理策略。难度2700。题意简述给定一棵\(n\)个点的树,每个节点上要么有一把钥匙,要么有一个宝箱,且钥匙和宝箱都是有颜......
  • CF1256E. Yet Another Division Into Teams 题解 单调队列优化dp
    题目链接:https://codeforces.com/contest/1256/problem/E将\(n\)个数分成若干队,每一队至少包含\(3\)个整数,每一队的代价是最大值与最小值只差,求所有队伍代价之和的最......
  • CF472D题解
    原题CF472DDesignTutorial:InversetheProblem思路概述题意分析给定一个\(n\)点无向图的两两点对之间距离,即经过最短路算法后的邻接矩阵,要求判断原图是否为一个......
  • Educational Round 30 题解
    ContestLink虽然是unrated,不过秉持着EducationalRound的传统,题还是挺不错的。A.ChoresProblemLink评价:善用STL。由于\(a\)已经排好序了,且\(x\le\min_{i=1......
  • CF1415D XOR-gun 题解 二分答案/贪心
    题目链接https://codeforces.com/problemset/problem/1415/D题目大意给定一个长为\(n\)的不降序列,每次操作可以任选相邻的两个数,并将这两个数替换为两个数按位异或的......
  • C语言基础笔试题解析
    题目在这里:​​c语言笔试面试大全,C语言基础笔试题_Thomas杨大炮的博客-CSDN博客t​​2.C语言程序的三种基本结构都有哪些呢?3. ​​递归调用​​和间接递归调用​​定义​......
  • CF1728A Colored Balls: Revisited 题解
    【题目传送门】思路因为球的总数为奇数,所以肯定会剩下一颗球,因此每次都往数量小的拿,那么最后剩下的球一定是最初数量最多的小球的编号。因为假设最多的少一颗,那么将可以......