题目链接:传送门
题意:
给你n个串
问你任意两个串的最大公共前缀长度是多少
题解:
二分+hash
思路很明显,我最近用来写hash
很鸡肋
#include<bits/stdc++.h> using namespace std; #pragma comment(linker, "/STACK:102400000,102400000") #define ls i<<1 #define rs ls | 1 #define mid ((ll+rr)>>1) #define pii pair<int,int> #define MP make_pair typedef long long LL; typedef unsigned long long ULL; const long long INF = 1e18+1LL; const double pi = acos(-1.0); const int N = 2e5+10, M = 1e3+20,inf = 2e9; ULL mod = 10000019ULL; vector<ULL > has[N]; int n,q; char ch[N]; int main() { int T,cas = 1; scanf("%d",&T); while(T--) { scanf("%d",&n); for(int i = 1; i <= n; ++i) has[i].clear(); for(int i = 1; i <= n; ++i) { scanf("%s",ch); int len = strlen(ch); ULL now = 0; ULL ps = 1; for(int j = 0; j < len; ++j) { has[i].push_back(0); has[i][j] = now + ps * ch[j]; now = has[i][j]; ps *= mod; } } scanf("%d",&q); printf("Case %d:\n",cas++); for(int i = 1; i <= q; ++i) { int x,y; scanf("%d%d",&x,&y); int ans = -1; int ll = 0, rr = min(has[x].size(),has[y].size()) - 1; while(ll <= rr) { if(has[x][mid] == has[y][mid]) ans = mid,ll = mid+1; else rr = mid - 1; } printf("%d\n",ans+1); } } return 0; }