首页 > 其他分享 >"蔚来杯"2022牛客暑期多校训练营9 G Magic Spells

"蔚来杯"2022牛客暑期多校训练营9 G Magic Spells

时间:2022-08-15 22:13:08浏览次数:91  
标签:cnt Magic int 蔚来 rep 多校 hw ++ str

原题链接

一开始manacher + 单哈希wa,样例通过率97%,应该是卡了一手int_64自然溢出
换成manacher + 双哈希过了

#include<bits/stdc++.h>
using namespace std;

#define fr first
#define se second
#define et0 exit(0);
#define rep(i, a, b) for(int i = (int)(a); i <= (int)(b); i ++)
#define rrep(i, a, b) for(int i = (int)(a); i >= (int)(b); i --)
#define IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);

typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
typedef unsigned long long ULL;
typedef pair<ULL, ULL> PUU;


const int INF = 0X3f3f3f3f, N = 700000 + 10, MOD1 = 1e9 + 7, MOD2 = 998244353, P = 131;
const double eps = 1e-7, pi = acos(-1);

int K = 0, n, len, cnt, hw[N];

ULL p1[N], p2[N], h1[N], h2[N];

char s[N], str[N];

map<PUU, int> st;

PUU query(int l, int r) {
	PUU Hash;
	Hash.fr = (h1[r] - h1[l - 1] * p1[r - l + 1] % MOD1 + MOD1) % MOD1;
	Hash.se = (h2[r] - h2[l - 1] * p2[r - l + 1] % MOD2 + MOD2) % MOD2;
	return Hash;
}

void Init() {
	p1[0] = p2[0] = 1;
	rep (i, 1, N - 1) p1[i] = p1[i - 1] * P % MOD1;
	rep (i, 1, N - 1) p2[i] = p2[i - 1] * P % MOD2;
}

void manacher() {
	K++;
	PUU hash_u;
	cnt = 2;
	str[0] = '&', str[1] = '*';
	rep (i, 0, len - 1) str[cnt++] = s[i], str[cnt++] = '*';
	str[cnt++] = '$';
	
	rep (i, 1, cnt - 1) h1[i] = (h1[i - 1] * P % MOD1 + str[i]) % MOD1;
	rep (i, 1, cnt - 1) h2[i] = (h2[i - 1] * P % MOD2 + str[i]) % MOD2;
	
	int mid = 0, maxright = 0;
	
	rep (i, 1, cnt - 2) {
		if (i < maxright) hw[i] = min(hw[mid * 2 - i], maxright - i + 1);
		else hw[i] = 1;
		hash_u = query(i, i + hw[i] - 1);
		if (str[i + hw[i] - 1] != '*' && st[hash_u] == K - 1) {
			st[hash_u]++;
		}
		while (str[i - hw[i]] == str[i + hw[i]]) {
			hw[i]++;
			hash_u = query(i, i + hw[i] - 1);
			if (str[i + hw[i] - 1] != '*' && st[hash_u] == K - 1) {
				st[hash_u]++;
			}
		}
		if (maxright < i + hw[i] - 1) maxright = i + hw[i] - 1, mid = i;
	}
}

void work() {
	Init();
	
	cin >> n;
	rep (i, 1, n) {
		scanf("%s", s);
		len = strlen(s);
		manacher();
	}
	
	int res = 0;
	for(auto cur : st) if (cur.se == n) res++;
	cout << res << endl;
}

signed main() {
	int test = 1;
	//	cin >> test;
	
	while (test--) {
		work();
	}
	
	return 0;
}

标签:cnt,Magic,int,蔚来,rep,多校,hw,++,str
From: https://www.cnblogs.com/xhy666/p/16589831.html

相关文章

  • Apple Magic Trackpad All In One
    AppleMagicTrackpadAllInOneApple妙控板AllInOne妙控板-白色多点触控表面RMB899妙控板-黑色多点触控表面RMB1,049https://www.apple.com.cn/shop/......
  • 2022杭电多校第八场1、7、5
    1001Theramore观察以下两种情况:以0为例,上图就是说,只要有两个连续的0,我们就可以一直把它们往前移动直到移动到首位。同理只要有两个连续的1我们就可以把它们移动到尾部......
  • "蔚来杯"2022牛客暑期多校训练营7 题解
    C.ConstructiveProblemsNeverDie对于出现次数大于1的数字,用出现次数为0的数字填充。剩下的数字一定两两互不相同,对这些数循环移位,最后进行判断即可。#include<bits/......
  • 2022HDU多校第七场
    2022HDU多校第七场过程本场队友上场秒了08,是昨天刚出现的nim博弈,随后04模拟分类讨论,我巨大演员wa了2发过了,随后03一眼树形dp,想了想计数方法,随后忘情况演了一发,然后就过了......
  • 2022.8.14 多校周报
    总结牛客第七场C开局签到题,一道构造,rty直接写了过了。F思维题,类似括号匹配,但当时没想到用栈实现,写的很麻烦WA了几次,最后rty用了个伪链表,还是做出来了。G被非常长的......
  • "蔚来杯"2022牛客暑期多校训练营7
    比赛链接:https://ac.nowcoder.com/acm/contest/33192C.ConstructiveProblemsNeverDie题意:已知序列\(a\),找出一个排列\(p\)使得\(a_i!=p_i(1<=i<=n)\)。......
  • 杭电多校杂题收录
    前言和学长学弟一起打的hdu多校,打的很菜没啥难题收录,因为难的我都不会做。正题hdu7152-Copy题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=7152题目大意\(n......
  • "蔚来杯"2022牛客暑期多校训练营8
     ABCDEFGHIJKL赛时过题            赛后补题            赛后总结:G题明明是很有希望做出......
  • 2022 杭电多校第八场 Vale of Eternal 凸包+找规律
    主要是存个代码,还有我踩的坑。。cin和cout真的很慢,很慢,非常慢..还有就是先把凸包求出来了,然后才能考虑凸包面积啥的刚开始思路错了,直接上多边形面积明明输出和标程都一......
  • "蔚来杯"2022牛客暑期多校训练营3
    A.Ancestor给定两棵有\(n\)个节点的树\(A、B\),树上节点均有一个权值,给出\(k\)个关键点的编号\(x_1,x_2,...,x_k\),问有多少种方案,使得恰好去掉一个关键点后,剩余关键点在\(A......