9.30考试总结

· · 个人记录

T1 [USACO06DEC] Milk Patterns G

题目描述有误,应该是“子串”或“连续子序列”,否则做不了。

赛时思路(正解)

如果一个子串出现了 x 次,那么这个子串的子串一定出现了至少 x 次。单调性成立,允许二分。

二分子串长度,O(n) 将字符串的每个子串的哈希值计入桶中,若有大于或等于 k 的即为成立,反之亦然。

赛时代码(AC)

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

#define int long long
#define ull unsigned long long
const int N = 2e4+5,Base = 1000003;
int n,k,a[N];
ull a_hash[N],pow_base[N];
map<ull,int>cnt;

ull get_hash(int l,int r) {return a_hash[r]-a_hash[l-1]*pow_base[r-l+1];}

bool check(int x)
{
    cnt.clear();
    for(int i = 1;i+x-1 <= n;i++)
    {
        int the_cnt = ++cnt[get_hash(i,i+x-1)];
        if(the_cnt >= k) return 1;
    }
    return 0;
}

signed main()
{
    ios::sync_with_stdio();
    cin.tie(0);
    cout.tie(0);
    cin >> n >> k;
    pow_base[0] = 1;
    for(int i = 1;i <= n;i++)
    {
        cin >> a[i];
        a_hash[i] = a_hash[i-1]*Base+a[i];
        pow_base[i] = pow_base[i-1]*Base;
    }
    int l = 0,r = n+1;
    while(l+1 < r)
    {
        int mid = (l+r)/2;
        if(check(mid)) l = mid;
        else r = mid;
    }
    cout << l;
    return 0;
}

T2 回文串计数

赛时思路(暴力)

计算字符串中每个位置中心扩张的结果,接着用 O(n^2) 枚举回文串计算答案。

正解思路

显然回文串可以用哈希判定。赛时把它想复杂了,用双重循环枚举 [l,r],用前缀和处理 [L,R],可以解决 l\le r<L\le R 的限制。

赛时代码(0pts)

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

#define int long long
const int N = 4e3+5;
int n,d[N],ans = 0;
string s;

signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin >> s;
    for(int i = 0;i < s.size();i++)
    {
        s.insert(s.begin()+i,'#');
        i++;
    }
    s += '#';
    n = s.size();
    s = " "+s+" ";
    for(int i = 1;i <= n;i++)
    {
        d[i] = 1;
        while(0 <= i-d[i] && i+d[i] <= n && s[i-d[i]] == s[i+d[i]]) d[i]++;
    }
    for(int i = 1;i <= n;i++)
    {
        for(int j = i+1;j <= n;j++)
        {
            int mid = j-i;
            for(int k = 1;k <= min(mid,d[i]);k++)
            {
                if(s[i-k] != '#') ans += (min({d[j],mid,mid-k})/2);
            }
        }
    }
    cout << ans;
    return 0;
}

正解代码

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

#define int long long
#define ull unsigned long long
const int N = 2e3+5,Base = 31;
int n,d[N],ans = 0,sum[N];
ull l_hash[N],r_hash[N],pow_base[N];
string s;

ull get_lhash(int l,int r) {return l_hash[r]-l_hash[l-1]*pow_base[r-l+1];}

ull get_rhash(int l,int r) {return r_hash[l]-r_hash[r+1]*pow_base[r-l+1];}

bool check(int l,int r) {return get_lhash(l,r) == get_rhash(l,r);}

signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin >> s;
    n = s.size();
    s = " "+s+" ";
    pow_base[0] = 1;
    for(int i = 1;i <= n;i++)
    {
        l_hash[i] = l_hash[i-1]*Base+s[i]-'a';
        pow_base[i] = pow_base[i-1]*Base;
    }
    for(int i = n;i >= 1;i--) r_hash[i] = r_hash[i+1]*Base+s[i]-'a';
    for(int l1 = n;l1 >= 1;l1--)
    {
        sum[l1] = sum[l1+1];
        for(int r1 = l1;r1 <= n;r1++)
        {
            if(check(l1,r1))
            {
                ans += sum[r1+1];
                sum[l1]++;
            }
        }
    }
    cout << ans;
    return 0;
}

T3 [蓝桥杯 2024 国 B] 最长回文前后缀

赛时思路(错)

不用草稿纸,想着想着就想歪了。写法和题目完全对不上。居然能过样例。

正解思路

把 S 两边已经回文的前后缀单独计算答案,中间部分最佳删除(如果有的话)区间必然与中间部分的左右端点重回至少一个。怎么证明呢?如果删除区间不满足上述条件,那么要想有贡献,必须中间部分存在回文前后缀,否则贡献为 0,但这与删除原有回文前后缀的设定相矛盾。

前去掉已有回文前后缀,枚举删除区间的端点(另一个端点与中间部分端点重合),用二分+哈希计算。

赛时代码(0pts)

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

#define ull unsigned long long
const int N = 500005,Base = 31;
int n,ans = 0;
string s;

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin >> s;
    n = s.size();
    s = " "+s+" ";
    int pt = n;
    for(int i = 1;i <= n;i++)
    {
        while(pt > i && s[pt] != s[i]) pt--;
        if(pt == i) break;
        ans = i;
        pt--;
    }
    pt = 1;
    for(int i = n;i >= 1;i--)
    {
        while(pt < i && s[pt] != s[i]) pt++;
        if(pt == i) break;
        ans = max(ans,n-i+1);
        pt++;
    }
    cout << ans;
    return 0;
}

正解代码

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

#define ull unsigned long long
const int N = 500005,Base = 31;
int n,ans = 0;
string s;
ull l_hash[N],r_hash[N],pow_base[N];

ull get_lhash(int l,int r) {return l_hash[r]-l_hash[l-1]*pow_base[r-l+1];}

ull get_rhash(int l,int r) {return r_hash[l]-r_hash[r+1]*pow_base[r-l+1];}

int get_ans(int sl,int sr)
{
    int l = 0,r = (sr-sl+1)/2+1;
    while(l+1 < r)
    {
        int mid = (l+r)/2;
        if(get_lhash(sl,sl+mid-1) == get_rhash(sr-mid+1,sr)) l = mid;
        else r = mid;
    }
    return l;
}

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin >> s;
    n = s.size();
    s = " "+s+" ";
    pow_base[0] = 1;
    for(int i = 1;i <= n;i++)
    {
        l_hash[i] = l_hash[i-1]*Base+s[i]-'a';
        pow_base[i] = pow_base[i-1]*Base;
    }
    for(int i = n;i >= 1;i--) r_hash[i] = r_hash[i+1]*Base+s[i]-'a';
    int l = 1,r = n;
    while(l < r)
    {
        if(s[l] != s[r]) break;
        l++;
        r--;
    }
    if(l > r)
    {
        cout << n/2;
        return 0;
    }
    get_ans(6,9);
    for(int i = l;i <= r;i++) ans = max({ans,get_ans(l,i),get_ans(i,r)});
    cout << l-1+ans;
    return 0;
}

T4 [POI 2010] ANT-Antisymmetry

赛时思路

用哈希判断是否合法,暴力枚举区间计数。

正解思路

和判断回文串的本质是一样的,只是在存储反向哈希的数组中多加一个反码。它有回文串的性质,使用二分枚举中心点即可。

赛时代码(30pts)

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

#define int long long
#define ull unsigned long long
const int N = 5e5+5,base = 3;
int n,ans = 0;
string s;
ull l_hash[N],r_hash[N],pow_base[N];
map<ull,int>cnt;

ull get_lhash(int l,int r) {return l_hash[r]-l_hash[l-1]*pow_base[r-l+1];}

ull get_rhash(int l,int r) {return r_hash[l]-r_hash[r+1]*pow_base[r-l+1];}

signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> s;
    s = " "+s+" ";
    pow_base[0] = 1;
    for(int i = 1;i <= n;i++)
    {
        l_hash[i] = l_hash[i-1]*base+s[i]-'0';
        pow_base[i] = pow_base[i-1]*base;
    }
    for(int i = n;i >= 1;i--) r_hash[i] = r_hash[i+1]*base+1-(s[i]-'0');
    for(int l = 1;l <= n;l++)
    {
        for(int r = l+1;r <= n;r++) if(get_lhash(l,r) == get_rhash(l,r)) ans++;
    }
    cout << ans;
    return 0;
}

正解代码

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

#define int long long
#define ull unsigned long long
const int N = 5e5+5,base = 3;
int n,ans = 0;
string s;
ull l_hash[N],r_hash[N],pow_base[N];

ull get_lhash(int l,int r) {return l_hash[r]-l_hash[l-1]*pow_base[r-l+1];}

ull get_rhash(int l,int r) {return r_hash[l]-r_hash[r+1]*pow_base[r-l+1];}

signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> s;
    s = " "+s+" ";
    pow_base[0] = 1;
    for(int i = 1;i <= n;i++)
    {
        l_hash[i] = l_hash[i-1]*base+s[i]-'0';
        pow_base[i] = pow_base[i-1]*base;
    }
    for(int i = n;i >= 1;i--) r_hash[i] = r_hash[i+1]*base+1-(s[i]-'0');
    for(int i = 1;i < n;i++)
    {
        int l = -1,r = min(i,n-i+1);
        while(l+1 < r)
        {
            int mid = (l+r)/2;
            if(get_lhash(i-mid,i+mid+1) == get_rhash(i-mid,i+mid+1)) l = mid;
            else r = mid;
        }
        ans += l+1;
    }
    cout << ans;
    return 0;
}