首页 > 其他分享 >AtCoder Beginner Contest 284-F - ABCBAC(双哈希)

AtCoder Beginner Contest 284-F - ABCBAC(双哈希)

时间:2023-01-08 14:00:19浏览次数:66  
标签:AtCoder Beginner Contest int hashv fi c2 c1 define

F - ABCBAC

题目大意:

给定一个正整数n,和一个长度为2*n的字符串s
问s串能不能是由一个t串经过如下操作变成s串:

  1. t串的前i个字符
  2. t串的反转串
  3. t串的后(n-i)个字符

如果存在这样的t串,请输出t串和i,否则输出-1

解题思路:

双哈希匹配字符串,只需要线性的扫描s串的前n个字符,按照操作匹配即可

双哈希:

存两套base和mod,用pair<int,int>来存储,保证了发生冲突的可能性更低
板子:

#include<bits/stdc++.h>
using namespace std;
# define int long long
# define endl "\n"
# define mk(a,b) make_pair(a,b)
# define fi first
# define se second
const int N = 2010000, inf = 0x3f3f3f3f;

typedef pair<int,int> hashv;
const int mod1 = 1e9+7;
const int mod2 = 1e9+9;

hashv operator + (hashv a,hashv b){
	int c1 = a.fi+b.fi,c2 = a.se+b.se;
	if(c1>=mod1) c1 -= mod1;
	if(c2>=mod2) c2 -= mod2;
	return mk(c1,c2);
}

hashv operator - (hashv a,hashv b){
	int c1 = a.fi-b.fi,c2 = a.se-b.se;
	if(c1<0) c1 += mod1;
	if(c2<0) c2 += mod2;
	return mk(c1,c2);
}

hashv operator * (hashv a,hashv b){
	return mk(1ll*a.fi*b.fi%mod1,1ll*a.se*b.se%mod2);
}
hashv pw[N],s[N];
char tt[N];
void solve() {
	int n;
	cin>>n;
	cin>>(tt+1);
	hashv base = mk(13331,23333);
	pw[0] = mk(1,1);
	for(int i = 1;i <= n;++i){
		pw[i] = pw[i-1]*base;
		s[i] = s[i-1]*base+mk(tt[i],tt[i]);
	}

代码实现:

#include<bits/stdc++.h>
using namespace std;
# define int long long
# define endl "\n"
# define mk(a,b) make_pair(a,b)
# define fi first
# define se second
const int N = 2010000, inf = 0x3f3f3f3f;

typedef pair<int,int> hashv;
const int mod1 = 1e9+7;
const int mod2 = 1e9+9;

hashv operator + (hashv a,hashv b){
	int c1 = a.fi+b.fi,c2 = a.se+b.se;
	if(c1>=mod1) c1 -= mod1;
	if(c2>=mod2) c2 -= mod2;
	return mk(c1,c2);
}

hashv operator - (hashv a,hashv b){
	int c1 = a.fi-b.fi,c2 = a.se-b.se;
	if(c1<0) c1 += mod1;
	if(c2<0) c2 += mod2;
	return mk(c1,c2);
}

hashv operator * (hashv a,hashv b){
	return mk(1ll*a.fi*b.fi%mod1,1ll*a.se*b.se%mod2);
}

hashv pw[N],s[N],t[N];
char tt[N];
void solve() {
	int n;
	cin>>n;
	n = n*2;
	cin>>(tt+1);
	hashv base = mk(13331,23333);
	pw[0] = mk(1,1);
	for(int i = 1;i <= n;++i){
		pw[i] = pw[i-1]*base;
		s[i] = s[i-1]*base+mk(tt[i],tt[i]);
	}
	for(int i = n;i>=1;--i){
		t[i] = t[i+1]*base+mk(tt[i],tt[i]);
	}
	
	for(int i = 0;i<=n/2;++i){
		/*s1取前i个和后(n-i)个*/
		hashv s1 = s[i]*pw[n/2-i]+(s[n]-s[i+n/2]*pw[n/2-i]);
		/*s2取从i+1开始的n个*/ 
		hashv s2 = t[i+1]-t[i+1+n/2]*pw[n/2];
		//相同则输出 
		if(s1 == s2){
			for(int j = n/2-1;j>=0;--j){
				printf("%c",tt[i+1+j]);
			}
			puts("");
			printf("%lld\n",i);
			return;
		}
	}
	puts("-1");

}
int __;
signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	__ = 1;
//	cin >> __;
	while (__--) solve();


	return 0;
}

标签:AtCoder,Beginner,Contest,int,hashv,fi,c2,c1,define
From: https://www.cnblogs.com/empty-y/p/17034604.html

相关文章

  • AtCoder Beginner Conest 284 解题报告
    AtCoderBeginnerConest284解题报告\(\text{ByDaiRuiChen007}\)\(\text{ContestLink}\)A.SequenceofStrings模拟,时间复杂度\(\Theta(n)\)#include<bits/stdc......
  • 2023.1.7(Atcoder Beginner Contest 284)
    A.HappyNewYear2023Linkhttps://atcoder.jp/contests/abc284/tasks/abc284_dStatement将给定的\(N\)分解成\(N=p^2\cdotq\)的形式,其中\(p,q\)为两个不......
  • Atcoder ABC 284题解
    DHappyNewYear2023(枚举,时间复杂度计算)题意​ 给定\(n\\le\9\times10^{18}\),给出式子\(n=p^2\timesq\),该式子必定有解且有唯一解。请输出\(p\)和\(q\)......
  • Atcoder Beginner Contest ABC 284 Ex Count Unlabeled Graphs 题解 (Polya定理)
    题目链接弱化版(其实完全一样)u1s1,洛谷上这题的第一个题解写得很不错,可以参考直接边讲Polya定理边做这题问题引入:n颗珠子组成的手串,每颗珠子有两种不同的颜色,如果两......
  • Atcoder Beginner Contest ABC 284 Ex Count Unlabeled Graphs 题解 (Polya定理)
    题目链接弱化版(其实完全一样)u1s1,洛谷上这题的第一个题解写得很不错,可以参考直接边讲Polya定理边做这题问题引入:n颗珠子组成的手串,每颗珠子有两种不同的颜色,如果两......
  • Atcoder Beginner Contest 284总结
    前言第一次做出6道题。比赛过程A题白给,耗时\(\text{1min}\)。B题白给,然而突然忘了oddnumber是奇数还是偶数,于是翻译了一下。耗时\(\text{2mins}\)。C题直接......
  • AtCoder Beginner Contest 284
    A-SequenceofStrings(abc284a)题目大意顺序给定\(n\)个字符串,倒着顺序输出。解题思路模拟即可。神奇的代码#include<bits/stdc++.h>usingnamespacestd;u......
  • E - Don't Isolate Elements -- ATCODER
    E-Don'tIsolateElementshttps://atcoder.jp/contests/abc283/tasks/abc283_e 思路 参考https://www.cnblogs.com/cilinmengye/p/17008799.html ......
  • The 14th Jilin Provincial Collegiate Programming Contest(补题)
    The14thJilinProvincialCollegiateProgrammingContest(补题)题目A这个题目我理解错了,题目里说的距离我以为是绝对值,没想到是差值,意思理解对了就很容易#include<io......
  • AtCoder Beginner Contest 257 题解
    AtCoderBeginnerContest257Solution目录AtCoderBeginnerContest257Solution更好的阅读体验戳此进入题面链接题面Luogu链接abc跳了[ABC257D]JumpingTakahashi......