UVALive - 4513 Stammering Aliens
2016-08-21 01:09
351 查看
1.题面
https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=25142,题意
给定一个字符串和一个数字m, 要求找出最少出现m次的最长的字符串,这些字符串之间可以重叠3.思路
使用字符串hash或是后缀数组4.代码
/***************************************************************** > File Name: cpp_acm.cpp > Author: Uncle_Sugar > Mail: uncle_sugar@qq.com > Created Time: Sat 20 Aug 2016 23:14:46 CST *****************************************************************/ # include <cstdio> # include <cstring> # include <cctype> # include <cmath> # include <cstdlib> # include <climits> # include <iostream> # include <iomanip> # include <set> # include <map> # include <vector> # include <stack> # include <queue> # include <algorithm> using namespace std; # define rep(i,a,b) for (i=a;i<=b;i++) # define rrep(i,a,b) for (i=b;i>=a;i--) template<class T>void PrintArray(T* first,T* last,char delim=' '){ for (;first!=last;first++) cout << *first << (first+1==last?'\n':delim); } const int debug = 1; const int size = 10 + 40000; const int INF = INT_MAX>>1; typedef unsigned long long ll; const ll MOD = 1000000007; /* 1.see the size of the input data before you select your algorithm 2.cin&cout is not recommended in ACM/ICPC 3.pay attention to the size you defined, for instance the size of edge is double the size of vertex */ ll Count[size]; char s[size]; int len = 0; ll hhash[size]; void pretreat(){ hhash[0] = 0; for (int i = 1; i <= len; i++){ hhash[i] = hhash[i-1] + (s[i-1] - 'a' + 1)*Count[i]; } } ll key[size]; int rrank[size]; bool cmp(int a, int b){ return key[a] < key[b]; } int check(int m, int c){ for (int i = m; i <= len; i++){ rrank[i] = i; key[i] = (hhash[i] - hhash[i - m])*Count[len-i]; } sort(rrank + m, rrank + len + 1, cmp); int cnt = 0, pos = 0; int right_most = -1; for (int i = m; i <= len; i++){ if (i==m||key[rrank[i]] != key[rrank[i-1]]) { pos = rrank[i]; cnt = 1; }else { pos = max(rrank[i], pos); cnt++; } if (cnt >= c) right_most = max(right_most, pos); } return right_most; } int main() { /*std::ios::sync_with_stdio(false);cin.tie(0);*/ Count[0] = 1; for (int i = 1; i < size; i++) Count[i] = Count[i-1]*MOD; int m; while (scanf("%d",&m) != EOF){ if (!m) break; scanf("%s", s); len = strlen(s); pretreat(); int l = 0, r = len + 1; int anspos = 0; while (l < r - 1){ int mid = l+r>>1; int k = check(mid, m); if (k != -1){ l = mid; anspos = k; }else { r = mid; } } if (l == 0) printf("none\n"); else printf("%d %d\n",l ,anspos-l); } return 0; }
相关文章推荐
- Uvalive 4513 Stammering Aliens(字符串Hash)
- HDU 4080 UVaLive 4513 - Stammering Aliens (字符串hash)
- uvalive 4513 Stammering Aliens (基于Hash的LCP+二分)
- UVALive4513 Stammering Aliens(哈希法,后缀数组)
- UVALive 4513 Stammering Aliens 字符串Hash求LCS
- UVALive 4513 Stammering Aliens
- UVALive 4513 Stammering Aliens 字符串Hash求LCS
- UVALive 4513 Stammering Aliens hash+二分
- UVALive - 4513 Stammering Aliens(后缀数组模板)
- UVALive 4513 (LA 4513) Stammering Aliens 后缀数组 或 hash
- UVALive4513 Stammering Aliens(哈希法,后缀数组)
- UVAlive 4763 Sudoku Extension【DLX】
- UVALive 6913 I Want That Cake 博弈+dp
- LA 3266 || UVALive 3266 Tian Ji -- The Horse Racing 田忌赛马(贪心)
- UVALive 6835
- UVA Live 7957 (Gym 101201F) Illumination 2-SAT
- uvalive2965Jurassic Remains
- UVALive 3263 That Nice Euler Circuit 计算几何+欧拉定理
- UVALive 4839 HDU 3686 Traffic Real Time Query System
- UVALive 6912 Prime Switch 状压DP,贪心