Array merging
题意:
给出两个长度为n的数组a,b,现在每次可以取出任意一个数组的第一个元素,放到c数组的后面,c数组一开始为空,求c数组连续相等的最长子串长度。
思路:
这里可以用两个map把a,b数组每个元素对应的连续相等的最长子串的长度存起来,然后找到最大值即可,具体看代码。
代码:
int n, a[maxn], b[maxn];
void solve() {
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1; i <= n; i++) cin >> b[i];
map<int, int> mp1, mp2;
for (int i = 1; i <= n; i++) {
int cnt = 1;
while (i + 1 <= n && a[i] == a[i + 1]) cnt++, i++;
mp1[a[i]] = max(mp1[a[i]], cnt);
}
for (int i = 1; i <= n; i++) {
int cnt = 1;
while (i + 1 <= n && b[i] == b[i + 1]) cnt++, i++;
mp2[b[i]] = max(mp2[b[i]], cnt);
}
int ans = 0;
for (auto p : mp1) ans = max(ans, p.second + mp2[p.first]);
for (auto p : mp2) ans = max(ans, p.second + mp1[p.first]);
cout << ans << endl;
}