标签:nsis width clear pac ring 二分 sha algo 相同
Input
Output
Example
input | output |
---|---|
5 10 ljkfghdfas isdfjksiye pgljkijlgp eyisdafdsi lnpglkfkjl |
3 1 1 3 3 |
先要对每一个字符串进行哈希预处理,哈希处理后,把哈希的值在根据行数哈希一遍
没存过的就存一遍, 找到有没有相同的,
这个二分还是比较好写的
这个二维哈希就有点懵逼
1 #include <iostream> 2 #include <cstdio> 3 #include <string> 4 #include <map> 5 #include <cstring> 6 #include <algorithm> 7 using namespace std; 8 9 typedef long long LL; 10 typedef unsigned long long ull; 11 typedef pair<int, int> ii; 12 const int maxn = 510; 13 const ull p = 1e12 + 7, pt = 19191919191919; 14 15 struct h { 16 ull ha[maxn][maxn], xp[maxn], sz; 17 void init1(int n) { 18 sz = n; 19 xp[0] = 1; 20 for (int i = 1 ; i <= sz ; i++) 21 xp[i] = xp[i - 1] * p; 22 } 23 void init2(int id, const string &str) { 24 ha[id][sz] = 0; 25 for (int i = sz - 1 ; i >= 0 ; i--) 26 ha[id][i] = ha[id][i + 1] * p + (str[i] - ‘a‘ + 1); 27 } 28 ull gethash(int id, int st, int len) { 29 return ha[id][st] - ha[id][st + len] * xp[len]; 30 } 31 } Hash; 32 string s[maxn]; 33 map< ull, ii>mp; 34 int n, m, ans; 35 ii ans1, ans2; 36 ull hat[maxn], xpt[maxn]; 37 void initxpt(int n) { 38 xpt[0] = 1; 39 for (int i = 1 ; i <= n ; i++ ) 40 xpt[i] = xpt[i - 1] * pt; 41 } 42 int check(int x) { 43 mp.clear(); 44 ull t; 45 for (int j = 0 ; j + x - 1 < m ; j++ ) { 46 hat[0] = 0; 47 for (int i = 1 ; i <= n ; i++) 48 hat[i] = hat[i - 1] * pt + Hash.gethash(i, j, x); 49 for (int k = x ; k <= n ; k++) { 50 t = hat[k] - hat[k - x] * xpt[x]; 51 if (mp.find(t) != mp.end()) { 52 ans1 = mp[t]; 53 ans2 = ii(k - x + 1, j + 1); 54 return 1; 55 } else mp[t] = ii(k - x + 1, j + 1); 56 } 57 } 58 return 0; 59 } 60 int main() { 61 cin >> n >> m; 62 Hash.init1(m); 63 for (int i = 1 ; i <= n ; i++) { 64 cin >> s[i]; 65 Hash.init2(i, s[i]); 66 } 67 initxpt(n); 68 ans = 0; 69 int l = 0, r = min(n, m) + 1, mid; 70 while(l <= r) { 71 mid = (l + r) >> 1; 72 if(check(mid)) { 73 ans = mid; 74 l = mid + 1; 75 } else r = mid - 1; 76 } 77 if(ans) cout << ans << "\n" << ans1.first << " " << ans1.second << "\n" << ans2.first << " " << ans2.second << endl; 78 else cout << ans << "\n"; 79 return 0; 80 }
URAL - 1486 Equal Squares 二维哈希+二分
标签:nsis width clear pac ring 二分 sha algo 相同
原文地址:https://www.cnblogs.com/qldabiaoge/p/9157229.html