跳到正文

题解

P8085 [COCI 2011/2012 #4] KRIPTOGRAM 详解

T0urist约 6 分钟
原文
文章信息与版本

文章信息

发布
2025年7月23日
收录
2025年12月12日
更新
2026年11月10日
篇幅
约 6 分钟
题目
P8085

版本

  1. v1当前

这道题简直是太妙了。

思路

离散讲的很好但是举的例子有错,而且代码 太难看。

去弄一一对应的关系显然不好实现,而且时间复杂度感性的想就不对。所以我们建立的匹配关系就是对应的单词在明文和密文中的距离是相同的!也就是到上一个相同单词的距离相等。利用哈希可以轻松处理距离,同时假设前面没有距离是 −1-1。

但是这时候如果直接再次利用哈希匹配,发现一个问题,由于明文很长,密文更短,可能在密文看来前面没有相同单词的单词,在明文中前面是有的,怎么解决呢?把密文看作是滑动窗口,滑出窗口的单词的下一个相同的单词距离变成 −1-1,这样就可以确保正确性了,实现的话,如果下一个相同单词在滑动窗口内,我们需要减去原来的贡献再加回来 −1-1 的贡献,否则我们直接修改距离数组就行了。

C++
//# pragma GCC optimize("Ofast")
# include <bits/stdc++.h>
# define fr front
# define il inline
# define fir first
# define sec second
# define vec vector
# define it iterator
# define pb push_back
# define lb lower_bound
# define ub upper_bound
# define all(x) x.begin(), x.end()
# define mem(a, b) memset(a, b, sizeof(a))

# define lc (t[p].l)
# define rc (t[p].r)
# define ls(x) (x << 1)
# define rs(x) (x << 1 | 1)
# define lson ls(p), l, mid
# define rson rs(p), mid + 1, r

# define sqr(x) ((x) * (x))
# define bpc __builtin_popcount
# define lowbit(x) ((x) & (-(x)))
# define geti(x, i) (((x) >> (i)) & 1)
# define set1(x, i) ((x) | (1 << (i)))
# define set0(x, i) ((x) & (~(1 << (i))))

# define debug1(x) cerr << #x << " = " << x << " "
# define debug2(x) cerr << #x << " = " << x << "\n"
# define bug cerr << "--------------------------\n"

# define each1(i, x) for(auto (i) : (x))
# define each2(i, x) for(auto (&i) : (x))
# define rep(i, a, b) for(int i = (a); i <= (b); ++ i)
# define pre(i, a, b) for(int i = (a); i >= (b); -- i)
# define G(i, h, u, ne) for(int i = (h[(u)]); i; i = (ne[(i)]))
# define reps(i, a, b, c) for(int i = (a); i <= (b); i += (c))
# define pres(i, a, b, c) for(int i = (a); i >= (b); i -= (c))
using namespace std;

using DB = double;
using LL = long long;
using LDB = long double;
using PII = pair<int, int>;
using ULL = unsigned long long;

const int N = 1e6 + 10, P = 131;
const int INF1 = 0x3f3f3f3f, INF2 = INT_MAX;
const LL INF3 = (LL)1e18, INF4 = 0x3f3f3f3f3f3f3f3f, INF5 = LLONG_MAX;

int n, m, len;
int lst[N], nxt[N], dis[N];

ULL h, g, p[N];

string s;

map<ULL, int> mp;

signed main(){
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
	
	while(cin >> s && s[0] != '$'){
		len = s.size() - 1;
		ULL h = 0;
		rep(i, 0, len) h = h * P + s[i];
		++ n;
		if(!mp.count(h)) lst[n] = -1;
		else lst[n] = n - mp[h], nxt[mp[h]] = n;
		mp[h] = n;
	}
	
	mp.clear();
	while(cin >> s && s[0] != '$'){
		len = s.size() - 1;
		ULL h = 0;
		rep(i, 0, len) h = h * P + s[i];
		++ m;
		if(!mp.count(h)) dis[m] = -1;
		else dis[m] = m - mp[h];
		mp[h] = m;
	}
	
	p[0] = 1;
	rep(i, 1, m) p[i] = p[i - 1] * P, g = g * P + dis[i];
	
	h = 0;
	rep(i, 1, m) h = h * P + lst[i];
	
	rep(i, 1, n - m + 1){
		int j = i + m - 1;
		if(h == g){
			cout << i;
			return 0;
		}
		
		if(i < nxt[i] && nxt[i] <= j){
			h = h - p[m - nxt[i] + i - 1] * lst[nxt[i]];//减去原来的  
			lst[nxt[i]] = -1;
			h = h - p[m - nxt[i] + i - 1];//-1的贡献 
		}
		
		else lst[nxt[i]] = -1;
		
		h = h - p[m - 1] * lst[i];
		h = h * P + lst[j + 1];
	}
	
	return 0;
}

Melody,無數動人音符在生命裡,愛過你,失去後我才知道要珍惜你…………

原文
存档评论1 条抓取于