8.21-401-下午
AmikazeMinato · · 个人记录
T1 兔子与兔子(P10468)
题意:给定一个字符串
思路:
字符串哈希模板题。预处理字符串的前缀哈希值,每次询问
代码:
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int INF=1e15+1e9+7;
const int base=131,MOD=1e9+7;
string s;
int m,pw[3000006];
unsigned int qzh[3000006];
signed main(){
cin>>s>>m;
s='&'+s;
pw[0]=1;
for(int i = 1;i < s.size();i++){
qzh[i] = (qzh[i - 1] * base + s[i]);
pw[i] = pw[i - 1] * base;
}
while(m--){
int l1,r1,l2,r2;
cin>>l1>>r1>>l2>>r2;
cout<<(qzh[r1]-qzh[l1-1] * pw[r1-l1+1]== qzh[r2]-qzh[l2-1] * pw[r2-l2+1]? "Yes" : "No")<<endl;
}
return 0;
}
T2 匹配统计(P10479)
题意:给定字符串
思路:
对
代码:
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int INF=1e15+1e9+7;
const int base=131,MOD=1e9+7;
int n,m,q,pw[3000006];
unsigned int qzh[3000006],qzh1[3000006];
string a,b;
map<int,int> mp;
unsigned int gethash(unsigned int qzh[],unsigned int l,unsigned int r){
return qzh[r]-qzh[l-1]*pw[r-l+1];
}
bool check(int mid,int i){
if(mid>m) return 0;
if(gethash(qzh,i,i+mid-1)==gethash(qzh1,1,mid)) return 1;
return 0;
}
int get(int inde){
int l=0,r=max(n,m);
while(l+1<r){
int mid=(l+r)>>1;
if(check(mid,inde)){
l=mid;
}else{
r=mid;
}
}
return l;
}
signed main(){
cin>>n>>m>>q;
cin>>a>>b;
a='&'+a;
b='&'+b;
pw[0]=1;
for(int i = 1;i<=max(n,m);i++){
pw[i] = pw[i - 1] * base;
}
for(int i = 1;i <= n;i++){
qzh[i] = (qzh[i - 1] * base + a[i]);
}
for(int i = 1;i <= m;i++){
qzh1[i] = (qzh1[i - 1] * base + b[i]);
}
for(int i = 1;i<=n;i++){
mp[get(i)]++;
}
while(q--){
int x;cin>>x;
cout<<mp[x]<<endl;
}
return 0;
}
T3 M形字符串(U577114)
题意:给定长度为
思路:
一个串是 M 形串当且仅当它本身是回文串,且其前半部分也是回文串。预处理正反两个方向的哈希值,枚举每个前缀长度
代码:
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int base=131;
string s;
unsigned int pw[3000006];
unsigned int qzh[3000006], fqzh[3000006];
unsigned int get_hash(int l, int r) {
if(l > r) return 0;
return qzh[r] - qzh[l-1] * pw[r-l+1];
}
unsigned int get_fhash(int l, int r) {
if(l > r) return 0;
return fqzh[l] - fqzh[r+1] * pw[r-l+1];
}
signed main() {
cin >> s;
s = '*' + s;
int n = s.size() - 1;
pw[0] = 1;
for(int i = 1; i <= n; i++) {
pw[i] = pw[i-1] * base;
qzh[i] = qzh[i-1] * base + (s[i] - 'a' + 1);
}
for(int i = n; i >= 1; i--) {
fqzh[i] = fqzh[i+1] * base + (s[i] - 'a' + 1);
}
int ans = 1;
for(int i = 2; i <= n; i++) {
if(i % 2 == 0) {
int mid = i / 2;
if(get_hash(1, mid) != get_hash(mid+1, i)) continue;
int mm = mid / 2;
if(mid % 2 == 0) {
if(get_hash(1, mm) == get_fhash(mm+1, mid)) {
ans++;
}
} else {
if(get_hash(1, mm) == get_fhash(mm+2, mid)) {
ans++;
}
}
} else {
int mid = i / 2;
if(get_hash(1, mid+1) != get_hash(mid+1, i)) continue;
int mm = (mid + 1) / 2;
if((mid + 1) % 2 == 0) {
if(get_hash(1, mm) == get_fhash(mm+1, mid+1)) {
ans++;
}
} else {
if(get_hash(1, mm) == get_fhash(mm+2, mid+1)) {
ans++;
}
}
}
}
cout << ans;
return 0;
}
T4 【模板】Manacher(P3805)
题意:给定一个长度为
思路:
字符串哈希 + 二分回文半径。枚举每个位置作为回文中心,对于奇数长度回文串,中心为单个字符;对于偶数长度回文串,需要额外判断相邻两个字符相同的情况。利用预处理的正反哈希值,二分判断两侧是否相等,更新答案。时间复杂度
对于此题数据,数据中最长回文子串的答案小的情况占多,可将二分长度换为暴力扩展长度,最差时间复杂度为
代码:
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int base = 131;
string s;
unsigned int pw[30000006];
unsigned int qzh[30000006], fqzh[30000006];
unsigned int get_hash(int l, int r) {
if(l > r) return 0;
return qzh[r] - qzh[l-1] * pw[r-l+1];
}
unsigned int get_fhash(int l, int r) {
if(l > r) return 0;
return fqzh[l] - fqzh[r+1] * pw[r-l+1];
}
signed main(){
cin >> s;
s = '*' + s;
int n = s.size() - 1;
pw[0] = 1;
for(int i = 1; i <= n; i++) {
pw[i] = pw[i-1] * base;
qzh[i] = qzh[i-1] * base + (s[i] - 'a' + 1);
}
for(int i = n; i >= 1; i--) {
fqzh[i] = fqzh[i+1] * base + (s[i] - 'a' + 1);
}
int ans = 0;
for(int i = 1; i <= n; i++) {
int len = 0;
while(i - len - 1 >= 1 && i + len + 1 <= n && get_hash(i-len-1, i-1) == get_fhash(i+1, i+len+1)) {
len++;
}
ans = max(ans, len * 2 + 1);
}
for(int i = 1; i < n; i++) {
int len = 0;
while(i - len >= 1 && i + len + 1 <= n && get_hash(i-len, i) == get_fhash(i+1, i+len+1)) {
len++;
}
ans = max(ans, len * 2);
}
cout << ans;
return 0;
}