比赛链接:

https://ac.nowcoder.com/acm/contest/33193

F.Longest Common Subsequence

题意:

给定长为 \(n\) 的序列 \(s\) 和长为 \(m\) 的序列 \(t\),求两个序列的最长公共子序列。

思路:

两个序列有一个位置相同后,后面肯定都是相同的,所以记录每个数第一次出现的位置,然后长度的最大即可。

代码:

#include <bits/stdc++.h>
using namespace std;
#define LL long long
void solve(){
	LL n, m, p, x, a, b, c;
	cin >> n >> m >> p >> x >> a >> b >> c;
	map <LL, LL> pos;
	for (int i = 0; i < n; i ++ ){
		x = (a % p * x % p * x % p + b % p * x % p + c % p) % p;
		if (!pos.count(x)){
			pos[x] = i;
		}
	}
	LL ans = 0;
	for (int i = 0; i < m; i ++ ){
		x = (a % p * x % p * x % p + b % p * x % p + c % p) % p;
		if (pos.count(x)){
			ans = max(ans, min(n - pos[x], m - i));
		}
	}
	cout << ans << "\n";
}
int main(){
	ios::sync_with_stdio(false);cin.tie(0);
	LL T = 1;
	cin >> T;
	while(T -- ){
		solve();
	}
	return 0;
}
posted on 2022-08-17 10:49  Hamine  阅读(33)  评论(0)    收藏  举报