8.21-401-下午

· · 个人记录

T1 兔子与兔子(P10468)

题意:给定一个字符串 S,有 m 次询问,每次询问两个区间 [l_1, r_1] 和 [l_2, r_2],判断这两个子串是否完全相同。

思路:
字符串哈希模板题。预处理字符串的前缀哈希值,每次询问 O(1) 求出两个子串的哈希值并比较即可。注意使用自然溢出或双模数避免冲突。

代码:

#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)

题意:给定字符串 A(长度 N)和 B(长度 M),对 A 的每个后缀子串 A[i..N],求其与 B 的最长公共前缀长度 L_i。有 Q 次询问,每次询问一个 X,输出 L_i = X 的位置数量。

思路:
对 A 和 B 分别预处理哈希值,枚举 A 的每个后缀起点 i,二分最长匹配长度。将答案统计到桶中,询问时直接输出桶内值。时间复杂度 O(N \log M + Q)。

代码:

#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)

题意:给定长度为 n 的字符串,求有多少个前缀满足:可以表示为两个相同的回文串拼接而成(两个回文串的边界字符可以重叠,即第二个回文串的第一个字符可以与第一个回文串的最后一个字符共用)。

思路:
一个串是 M 形串当且仅当它本身是回文串,且其前半部分也是回文串。预处理正反两个方向的哈希值,枚举每个前缀长度 i,先判断整个前缀是否为回文串,再判断其前半部分是否为回文串,两者都满足则计入答案。

代码:

#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)

题意:给定一个长度为 n 的字符串 S,求 S 中最长回文子串的长度。

思路:
字符串哈希 + 二分回文半径。枚举每个位置作为回文中心,对于奇数长度回文串,中心为单个字符;对于偶数长度回文串,需要额外判断相邻两个字符相同的情况。利用预处理的正反哈希值,二分判断两侧是否相等,更新答案。时间复杂度 O(n \log n),由于 n \le 1.1\times 10^7,该做法只能获得 24 分,正解为 Manacher 算法。
对于此题数据,数据中最长回文子串的答案小的情况占多,可将二分长度换为暴力扩展长度,最差时间复杂度为O(n^2),但可以获得66pts

代码:

#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;
}