8/28

· · 个人记录

P3763

思路:

因为可以修改 3 次 ,所以我们可以在枚举完子串的起点后 ,用三次二分 。每次找从这个点开始不用修改的最长长度 。让后将起点更新到下一个点 ,记得要跳过要修改的点 。此次类推 。

代码:

#include<bits/stdc++.h>

using namespace std;

const int N = 1e6 + 5;
const int base = 13331;

int t;
unsigned long long h0[N], hs[N], pw[N];

unsigned long long get_hs(unsigned long long hs[], int l, int r)
{
    return hs[r] - hs[l - 1] * pw[r - l + 1];
}

bool check(int x, int ed)
{
    int l = 1, r = x + ed - 1;

    for(int i = 1; i <= 3; i++)
    {
        int lt = -1, rt = ed + 1;

        while(lt + 1 < rt)
        {
            int mid = (lt + rt) / 2;

            if(get_hs(hs, l, l + mid - 1) == get_hs(h0, x, x + mid - 1))
            {
                lt = mid;
            }
            else
            {
                rt = mid;
            }
        }

        l = l + lt + 1;
        x = x + lt + 1;

        if(x > r)
        {
            return 1;
        }
    }

    return get_hs(hs, l, ed) == get_hs(h0, x, r);
}

void solve()
{
    string s0, s;
    cin >> s0 >> s;

    int n = s0.size(), m = s.size();
    s0 = '#' + s0;
    s = '#' + s;

    if(n < m)
    {
        cout << 0 << "\n";
        return ;
    } 

    for(int i = 1; i <= n; i++)
    {
        h0[i] = h0[i - 1] * base + s0[i];
    }
    for(int i = 1; i <= m; i++)
    {
        hs[i] = hs[i - 1] * base + s[i];
    }

    int ans = 0;

    for(int i = 1; i + m - 1 <= n; i++)
    {
        if(check(i, m) == 1)
        {
            ans++;
        }
    }

    cout << ans << "\n";
    return ;
}

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0); 

    cin >> t;

    pw[0] = 1;
    for(int i = 1; i <= 1e5; i++)
    {
        pw[i] = pw[i - 1] * base;
    }

    while(t--)
    {
        solve();
    }
    return 0;
}

P1872

思路:

定义 cnt1[r] 表示恰好以 r 结尾的回文串的个数 ,cnt[l] 表示恰好以 l 开头的回文字符串的个数 。接下来我们可以枚举 r ,那么 r 的贡献就为 cnt1[r] 乘以所有 r 到 n 的 cnt[] 的和 。所以我们可以维护 cnt[] 后缀和 。然后我们再维护一个前缀和后缀哈希 ,这样我们判回文时就可以更高效地解决了 ,cnt1[] 和 cnt[] 也就可以求出来了 。

代码:

#include<bits/stdc++.h>

using namespace std;

const int N = 2005;
const int base = 13331;

string s;
int n, cnt[N], cnt1[N], suf[N];
unsigned long long hs[N], ht[N], pw[N];

unsigned long long get_hs(int l, int r)
{
    return hs[r] - hs[l - 1] * pw[r - l + 1];
}
unsigned long long get_suf_hs(int l, int r)
{
    return ht[l] - ht[r + 1] * pw[r - l + 1];   
} 

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0); 

    cin >> s;
    n = s.size();

    s = '#' + s;

    pw[0] = 1;
    for(int i = 1; i <= n; i++)
    {
        pw[i] = pw[i - 1] * base;
        hs[i] = hs[i - 1] * base + s[i];
    }
    for(int i = n; i >= 1; i--)
    {
        ht[i] = ht[i + 1] * base + s[i];
    }

    for(int l = 1; l <= n; l++)
    {
        for(int r = l; r <= n; r++)
        {
            if(get_hs(l, r) == get_suf_hs(l, r))
            {
                cnt[l]++, cnt1[r]++;    
            }       
        }
    } 

    for(int i = n; i >= 1; i--)
    {
        suf[i] = suf[i + 1] + cnt[i];
    }

    long long ans = 0;

    for(int i = 1; i <= n; i++)
    {
        ans += cnt1[i] * suf[i + 1];
    }

    cout << ans;
    return 0;
}

P3501

思路:

这题我们可以枚举中间的轴 ,让后再用二分求出最长的半径 ,也相当于个数 ,最终让答案加起来即可 。

代码:

#include<bits/stdc++.h>

using namespace std;

const int N = 5e5 + 5;
const int base = 13331;

int n;
string s;
unsigned long long hs[N], ht[N], pw[N];

unsigned long long get_hs(int l, int r)
{
    return hs[r] - hs[l - 1] * pw[r - l + 1];
}
unsigned long long get_suf_hs(int l, int r)
{
    return ht[l] - ht[r + 1] * pw[r - l + 1];   
}

bool check(int i, int x)
{
    int l = i - x + 1, r = i + x;

    if(get_suf_hs(l, r) == get_hs(l, r))
    {
        return 1;
    }
    return 0;
}

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0); 

    cin >> n >> s;

    s = '#' + s;
    string s2 = "#";

    pw[0] = 1;
    for(int i = 1; i <= n; i++)
    {
        if(s[i] == '1')
        {
            s2 += '0';
        }
        else
        {
            s2 += '1';
        }
        pw[i] = pw[i - 1] * base;
        hs[i] = hs[i - 1] * base + s2[i];
    }
    for(int i = n; i >= 1; i--)
    {
        ht[i] = ht[i + 1] * base + s[i];
    }

    long long ans = 0;

    for(int i = 1; i < n; i++)
    {
        int lt = 0, rt = min(i, n - i) + 1;

        while(lt + 1 < rt)
        {
            int mid = (lt + rt) / 2;

            if(check(i, mid) == 1)
            {
                lt = mid;
            }
            else
            {
                rt = mid;
            }
        }

        ans += lt;
    }

    cout << ans;
    return 0;
}

P1481

思路:

这题很像最长上升子序列 ,我们只需要再判断是否能 dp 哪里改成用哈希看是否是前缀即可 。

代码:

#include<bits/stdc++.h>

using namespace std;

const int N = 2005;
const int base = 13331;

int n, dp[N];
string s[N];
unsigned long long a[N][N], pw[N];

unsigned long long get_hs(unsigned long long i, int l, int r)
{
    return a[i][r] - a[i][l - 1] * pw[r - l + 1];
}

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0); 

    cin >> n;

    pw[0] = 1;
    for(int i = 1; i <= 2000; i++)
    {
        pw[i] = pw[i - 1] * base;
    }

    for(int i = 1; i <= n; i++)
    {
        cin >> s[i];
        s[i] = '#' + s[i];

        for(int j = 1; j < s[i].size(); j++)
        {
            a[i][j] = a[i][j - 1] * base + s[i][j];
        }
    }

    for(int i = 1; i <= n; i++)
    {
        dp[i] = 1;

        for(int j = 1; j < i; j++)
        {
            if(get_hs(i, 1, s[j].size() - 1) == a[j][s[j].size() - 1])
            {
                dp[i] = max(dp[i], dp[j] + 1);
            }
        }
    }

    int ans = 0;

    for(int i = 1; i <= n; i++)
    {
        ans = max(ans, dp[i]);
    }

    cout << ans;
    return 0;
}