codeforces gym 101164 K Cutting 字符串hash

时间:2023-03-09 18:19:32
codeforces gym 101164 K Cutting 字符串hash

题意:给你两个字符串a,b,不区分大小写,将b分成三段,重新拼接,问是否能得到A;

思路:暴力枚举两个断点,然后check的时候需要字符串hash,O(1)复杂度N*N;

题目链接:传送门

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
using namespace std;
#define LL long long
#define pi (4*atan(1.0))
#define eps 1e-8
#define bug(x) cout<<"bug"<<x<<endl;
const int N=5e3+,M=2e6+,inf=1e9+;
const LL INF=1e18+,mod=1e9+; string a,b,c;
unsigned int ma[N][N],mc[N][N];
int check(int l1,int r1,int l2,int r2,int l3,int r3)
{
int s=,e=r1-l1;
if(ma[s][e]!=mc[l1][r1])return ;
s=e+,e=s+r2-l2;
if(ma[s][e]!=mc[l2][r2])return ;
s=e+,e=s+r3-l3;
if(ma[s][e]!=mc[l3][r3])return ;
return ;
}
void output(int l1,int r1,int l2,int r2,int l3,int r3)
{
for(int i=l1;i<=r1;i++)
printf("%c",b[i]);
printf("\n");
for(int i=l2;i<=r2;i++)
printf("%c",b[i]);
printf("\n");
for(int i=l3;i<=r3;i++)
printf("%c",b[i]);
printf("\n");
}
int main()
{
cin>>a>>b;
int n=a.size();c="";
for(int i=;i<n;i++)
if(a[i]>='A'&&a[i]<='Z')a[i]=a[i]-'A'+'a';
for(int i=;i<n;i++)
if(b[i]>='A'&&b[i]<='Z')c+=b[i]-'A'+'a';
else c+=b[i];
int tot=;
for(int i=;i<n;i++)
{
unsigned int seed = ; // 31 131 1313 13131 131313 etc..
unsigned int has= ;
for(int j=i;j<n;j++)
{
has=has*seed+(a[j]);
ma[i][j]=(has & 0x7FFFFFFF);
}
}
for(int i=;i<n;i++)
{
unsigned int seed = ; // 31 131 1313 13131 131313 etc..
unsigned int has= ;
for(int j=i;j<n;j++)
{
has=has*seed+(c[j]);
mc[i][j]=(has & 0x7FFFFFFF);
}
}
for(int i=;i<=n-;i++)
{
for(int j=i;j<=n-;j++)
{
if(check(,i-,i,j,j+,n-))
{
printf("YES\n");
output(,i-,i,j,j+,n-);
return ;
}
if(check(,i-,j+,n-,i,j))
{
printf("YES\n");
output(,i-,j+,n-,i,j);
return ;
}
if(check(i,j,,i-,j+,n-))
{
printf("YES\n");
output(i,j,,i-,j+,n-);
return ;;
}
if(check(i,j,j+,n-,,i-))
{
printf("YES\n");
output(i,j,j+,n-,,i-);
return ;;
}
if(check(j+,n-,,i-,i,j))
{
printf("YES\n");
output(j+,n-,,i-,i,j);
return ;
}
if(check(j+,n-,i,j,,i-))
{
printf("YES\n");
output(j+,n-,i,j,,i-);
return ;;
}
}
}
printf("NO\n");
return ;
}