题意:
给出一些串,询问任意两个的公共前缀。
分析:
对于每个串处理出所有前缀的HASH值,询问的时候二分公共前缀长度,比较HASH值是否相等。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
ull Hash, seed = 131;
const int maxn = 100105;
vector<ull> v[maxn];
char s[maxn];
int main()
{int t, n, m, a, b, kase = 1;scanf("%d", &t);while(t--){scanf("%d", &n);for(int i = 0; i < n; i++){scanf("%s", s);v[i+1].clear();for(int j = 0, Hash = 0; s[j]; j++){Hash = Hash * seed + s[j] - 'a';v[i+1].push_back(Hash);}}scanf("%d", &m);printf("Case %d:\n", kase++);while(m--){scanf("%d%d", &a, &b);int l = 0, r = min(v[a].size(), v[b].size())-1, mid, ans = 0;while(l <= r){int mid = (l + r) >> 1;if(v[a][mid] == v[b][mid])l = mid + 1, ans = mid;elser = mid - 1;}if(v[a][ans] != v[b][ans])printf("%d\n", ans);elseprintf("%d\n", ans+1);}}return 0;
}