跳到正文

题解

8.20课程题解

Eric0507约 7 分钟
原文
文章信息与版本

文章信息

发布
2026年8月21日
收录
2026年8月23日
更新
2026年11月9日
篇幅
约 7 分钟

版本

  1. v1当前

兔子题前面讲到了,所以这边不再做过多言论。

2.CF39J Spelling Check

思路:

非常简单,实际上就是去掉一个字符,然后再把前后两个字符串的哈希值拼起来,看看和第二个字符串一不一样就行了。

代码:

C++
#include<bits/stdc++.h>
#define ll long long
#define int unsigned long long
#define endl '\n'
using namespace std;
const int N=1e6+5;
string s, t;
int n, m;
int p=131;
int pw[N];
int hs[N];
int sum=0;
vector<int> ans;
void init()
{
	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 get_hash(int l, int r)
{
	return hs[r]-hs[l-1]*pw[r-l+1];
}
int del_hs(int x, int len)
{
	return get_hash(1, x-1)*pw[len-x]+get_hash(x+1, len);
}
signed main()
{
	ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
	cin >> s >> t;
	n=s.size();
	m=t.size();
	s='Y'+s;
	t='Y'+t;
	init();
	for(int i=1; i<=m; i++) sum=sum*p+t[i];
	for(int i=1; i<=n; i++)
	{
		if(del_hs(i, n)==sum)
		{
			ans.push_back(i);
		}
	}
	cout << ans.size() << endl;
	for(int i=0; i<ans.size(); i++) cout << ans[i] << " ";
	return 0;
}

3.P3370 【模板】字符串哈希

思路:

用正规哈希总有一种高射炮打蚊子的感觉

很简单,包每个字符串的哈希值都算出来,然后存进桶里。

你说桶不能开太大?用map存不就好了嘛。

搞半天还是要用map

代码:

C++
#include<bits/stdc++.h>
#define ll long long
#define int unsigned long long
#define endl '\n'
using namespace std;
int n;
int p=131;
map<int, bool> mp;
int pw[2005];
void init(string s)
{
	int sum=1;
	int len=s.size();
	s='Y'+s;
	for(int i=1; i<=len; i++) sum=sum*p+s[i];
	mp[sum]=1;
}
signed main()
{
	ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
	cin >> n;
	pw[0]=1;
	for(int i=1; i<=2000; i++) pw[i]=pw[i-1]*p;
	for(int i=1; i<=n; i++)
	{
		string s;
		cin >> s;
		init(s);
	}
	cout << mp.size();
	return 0;
}

4.P6739 [BalticOI 2014] Three Friends (Day1)

思路:

枚举每个点,然后去掉以后(不算哈希值)看看前后两个字符串的哈希值相不相等就完事了。

注意,这道题细节有点多,追重要的是check那里拼哈希值的时候不要出错了。

代码(老师求助,35pts,TLE,改不出来):

C++
#include<bits/stdc++.h>
#define ll long long
#define int unsigned long long
#define endl '\n'
using namespace std;
const int N=2e6+5, p=131;
int n;
string s;
int mid;
int pw[N], hs[N];
map<int, bool> mp;
int s1=0, s2=0;
int get_hash(int l, int r)
{
	if(l>r) return 0;
	return hs[r]-hs[l-1]*pw[r-l+1];
}
bool check(int x)
{
	s1=0, s2=0;
	if(x==mid)
	{
		s1=get_hash(1, mid-1);
		s2=get_hash(mid+1, n);
	}
	else if(x<mid)
	{
		s1+=get_hash(1, x-1)*pw[mid-x]+get_hash(x+1, mid);
		s2+=get_hash(mid+1, n);
	}
	else
	{
		s1+=get_hash(1, mid-1);
		s2+=get_hash(mid, x-1)*pw[n-x]+get_hash(x+1, n);
	}
	if(s1==s2) return 1;
	return 0;
}
string sub(int l, int r)
{
	string ans="";
	for(int i=l; i<=r; i++) ans=ans+s[i];
	return ans;
}
signed main()
{
	ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
    cin >> n;
	cin >> s;
	s='Y'+s;
	if(n%2==0)
	{
		cout << "NOT POSSIBLE";
		return 0;
	}
	pw[0]=1;
	for(int i=1; i<=n; i++)
	{
		pw[i]=pw[i-1]*p;
		hs[i]=hs[i-1]*p+s[i];
	}
	mid=n/2+1;
	int ans=0, x;
	for(int i=1; i<=n; i++)
	{
		if(check(i))
        {
            if(mp[s1]==0)
            {
                mp[s1]=1;
                ans++;
                x=i;
            }
        }
        if(ans>1)
        {
            cout << "NOT UNIQUE";
            return 0;
        }
	}
	if(ans>1) cout << "NOT UNIQUE";
	else if(ans==0) cout << "NOT POSSIBLE";
	else
	{
		if(x<mid) cout << sub(mid+1, n);
		else cout << sub(1, mid-1);
	}
	return 0;
}
原文