8.22课程题解

· · 题解

1.P6456 [COCI 2006/2007 #5] DVAPUT

思路:

这道题我们二分长度,然后check的时候用map存储每个字符串的哈希值,然后判断有没有一个哈希值出现两次就行了。

注意,普通map存入还需要时间,所以我们这里使用unordered_map。

代码:

#include<bits/stdc++.h>
#define ll long long
#define int unsigned long long
#define endl '\n'
using namespace std;
const int N=2e5+5, p=131;
int n;
string s;
unordered_map<int, int> mp;
int pw[N], hs[N];
int get_hash(int l, int r)
{
    return hs[r]-hs[l-1]*pw[r-l+1];
}
bool check(int mid)
{
    mp.clear();
    int s=0;
    for(int i=1; i<=n-mid+1; i++)
    {
        mp[get_hash(i, i+mid-1)]++;
        s=max(s, mp[get_hash(i, i+mid-1)]);
    }
    return s>=2;
}
signed main()
{
    ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
    cin >> n;
    cin >> s;
    s='Y'+s;
    pw[0]=1;
    for(int i=1; i<=n; i++) pw[i]=pw[i-1]*p;
    for(int i=1; i<=n; i++) hs[i]=hs[i-1]*p+s[i];
    int l=0, r=n+1;
    while(l+1<r)
    {
        int mid=(l+r)>>1;
        if(check(mid)) l=mid;
        else r=mid;
    }
    cout << l;
    return 0;
}

2.P3498 [POI 2010] KOR-Beads

思路:

非常简单,算道水青。

我们可以枚举长度,然后按题目说的去算。用map当桶嘛。然后这样的时间是O(nlogn)。

但是本体非常邪恶,会卡常,所以我们还要加个特判:如果长度已经大于n \div i了,直接跳过。

代码:

#include<bits/stdc++.h>
#define ll long long
#define int unsigned long long
#define endl '\n'
using namespace std;
const int N=2e5+5, p=19260817;
int n;
int a[N];
int pw[N], hs[N], hs2[N];
map<int, int> mp;
vector<int> ans;
int get_hash(int l, int r)
{
    return hs[r]-(hs[l-1]*pw[r-l+1]);
}
int get_hash2(int l, int r)
{
    return hs2[l]-(hs2[r+1]*pw[r-l+1]);
}
signed main()
{
    ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
    cin >> n;
    for(int i=1; i<=n; i++)
    {
        cin >> a[i];
    }
    pw[0]=1;
    for(int i=1; i<=n; i++) pw[i]=(pw[i-1]*p);
    for(int i=1; i<=n; i++) hs[i]=(hs[i-1]*p+a[i]);
    for(int i=n; i>=1; i--) hs2[i]=(hs2[i+1]*p+a[i]);
    int maxx=0;
    for(int i=1; i<=n; i++)
    {
        if(n/i<maxx) continue;
        int s=0;
        for(int j=1; j+i-1<=n; j+=i)
        {
            if(mp[get_hash(j, j+i-1)]==0 && mp[get_hash2(j, j+i-1)]==0)
            {
                s++;
            }
            mp[get_hash(j, j+i-1)]=1;
            mp[get_hash2(j, j+i-1)]=1;
        }
        if(maxx<s)
        {
            maxx=s;
            while(ans.size()) ans.pop_back();
            ans.push_back(i);
        }
        else if(maxx==s)
        {
            ans.push_back(i);
        }
        mp.clear();
    }
    cout << maxx << " " << ans.size() << endl;
    for(int i=0; i<ans.size(); i++) cout << ans[i] << " ";
    return 0;
}

3.AT_abc367_f [ABC367F] Rearrange Query

思路:

这道题实际上就是用前缀和。

但是普通前缀和太容易被卡了,所以我们可以加个k次方,这样就好了。

因为这边数据太水了,所以我没加次方就过了,赛场上千万不要这样。

代码:

#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define endl '\n'
using namespace std;
const int N=2e5+5;
int n, q;
int a[N], b[N];
int suma[N], sumb[N]; //实际上这里要加k次方
signed main()
{
    ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
    cin >> n >> q;
    for(int i=1; i<=n; i++) cin >> a[i];
    for(int i=1; i<=n; i++) cin >> b[i];
    for(int i=1; i<=n; i++)
    {
        suma[i]=suma[i-1]+a[i];
        sumb[i]=sumb[i-1]+b[i];
    }
    while(q--)
    {
        int l1, r1, l2, r2;
        cin >> l1 >> r1 >> l2 >> r2;
        if(suma[r1]-suma[l1-1]==sumb[r2]-sumb[l2-1]) cout << "Yes" << endl;
        else cout << "No" << endl;
        //查询到时候也要查询k次方
    }
    return 0;
}

4.P8023 [ONTAK2015] Tasowanie

思路:

二路归并的意思就是说可以从两个数组中任意选出第一个数,然后放到另一个数组中(选了以后当然没了)。

题目要求字典序最小,当然是两个数组的第一个数那个最小选哪个。

但是问题是如果两个一样选哪个。经过推断样例我们发现,往后不断找,第一个不相同的,然后看看那个更小,就选更小所在的数组的第一个数。

代码:

#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define endl '\n'
using namespace std;
const int N=2e5+5, p=131;
int n, m;
int a[N], b[N];
int pw[N], hs_a[N], hs_b[N];

int get_hash_a(int l, int r)
{
    return hs_a[r]-hs_a[l-1]*pw[r-l+1];
}
int get_hash_b(int l, int r)
{
    return hs_b[r]-hs_b[l-1]*pw[r-l+1];
}
signed main()
{
    ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
    cin >> n;
    for(int i=1; i<=n; i++) cin >> a[i];
    cin >> m;
    for(int i=1; i<=m; i++) cin >> b[i];
    pw[0]=1;
    for(int i=1; i<=max(n, m); i++) pw[i]=pw[i-1]*p;
    for(int i=1; i<=n; i++) hs_a[i]=hs_a[i-1]*p+a[i];
    for(int i=1; i<=m; i++) hs_b[i]=hs_b[i-1]*p+b[i];
    int now1=1, now2=1;
    while(now1<=n && now2<=m)
    {
        if(a[now1]!=b[now2])
        {
            if(a[now1]<b[now2]) cout << a[now1++] << " ";
            else cout << b[now2++] << " ";
        }
        else
        {
            int l=0, r=min(n-now1+1, m-now2+1)+1;
            while(l+1<r)
            {
                int mid=(l+r)>>1;
                if(get_hash_a(now1, now1+mid-1)==get_hash_b(now2, now2+mid-1))
                {
                    l=mid;
                }
                else
                {
                    r=mid;
                }
            }
            if(now1+l-1==n) cout << b[now2++] << " ";
            else if(now2+l-1==m) cout << a[now1++] << " ";
            else
            {
                if(a[now1+l]<b[now2+l]) cout << a[now1++] << " ";
                else cout << b[now2++] << " ";
            }

        }
    }
    while(now1<=n) cout << a[now1++] << " ";
    while(now2<=m) cout << b[now2++] << " ";
    return 0;
}