UVA - 12338 Anti-Rhyme Pairs 二分+hash
Posted Meek
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UVA - 12338 Anti-Rhyme Pairs 二分+hash相关的知识,希望对你有一定的参考价值。
题目链接:https://vjudge.net/problem/UVA-12338
题意:
给你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; }
以上是关于UVA - 12338 Anti-Rhyme Pairs 二分+hash的主要内容,如果未能解决你的问题,请参考以下文章