最小表示法+hash hdu2609 How many
来源:互联网 发布:英语口语书籍 知乎 编辑:程序博客网 时间:2024/05/22 09:38
传送门:点击打开链接
题意:有n个串,每个串是一个环,如果通过移动环之后能一样,认为是同一种串。问有多少种串。
思路:很明显我们首先要把认为是一样的串变成一样的,比如找到这个串的最小表示法,所以这道题的目的就是为了求最小表示法,然后用hash搞一搞排序然后去重,或者是直接插入到set里面取size都是可以的
KMP(复杂度O(n))来搞最小表示法暂时还不会,先挖个坑,我这里还有一种通过二分+hash求lcp来求最小表示法的思路,复杂度O(nlogn),先凑合着用把
#include <map>#include <set>#include <cmath>#include <ctime>#include <stack>#include <queue>#include <cstdio>#include <cctype>#include <bitset>#include <string>#include <vector>#include <cstring>#include <iostream>#include <algorithm>#include <functional>#define fuck(x) cout << "[" << x << "]"#define FIN freopen("input.txt", "r", stdin)#define FOUT freopen("output.txt", "w+", stdout)using namespace std;typedef long long LL;typedef pair<int, int> PII;typedef unsigned long long ULL;const int MX = 3e2 + 5;const int INF = 0x3f3f3f3f;const int seed = 131;ULL fac[MX], pre[MX], S[10005];char A[MX];void presolve() { fac[0] = 1; for(int i = 1; i < MX; i++) { fac[i] = fac[i - 1] * seed; }}bool check(int a, int b, int l) { ULL left = pre[a + l - 1] - pre[a - 1] * fac[l]; ULL right = pre[b + l - 1] - pre[b - 1] * fac[l]; return left == right;}int lcp(int n, int a, int b) { int l = 0, r = n, m; while(l <= r) { m = (l + r) >> 1; if(check(a, b, m)) l = m + 1; else r = m - 1; } return l - 1;}ULL change() { int n = strlen(A + 1); for(int i = 1; i <= n; i++) { A[n + i] = A[i]; } pre[0] = 0; for(int i = 1; i <= 2 * n; i++) { pre[i] = pre[i - 1] * seed + A[i]; } int ans = 1, m; for(int i = 1; i <= n; i++) { int l = lcp(n, ans, i); if(A[i + l] < A[ans + l]) ans = i; } ULL ret = 0; for(int i = ans, j = 1; j <= n; i++, j++) { ret = ret * seed + A[i]; } return ret;}int main() { int n; //FIN; presolve(); while(~scanf("%d", &n)) { for(int i = 1; i <= n; i++) { scanf("%s", A + 1); S[i] = change(); } sort(S + 1, S + 1 + n); int sz = unique(S + 1, S + 1 + n) - S - 1; printf("%d\n", sz); } return 0;}
0 0
- 最小表示法+hash hdu2609 How many
- hdu2609 How many (最小表示法)
- HDU2609 How many(最小表示法)
- hdu2609-字符串最小表示法|模拟|二分+lcp-How many
- HDU2609 How many(字符串的最小表示法)
- How many 杭电oj 2609 hdu2609 最小表示
- HDU2609 How many(字符串的最小表示+set)
- hdu2609(最小表示法)
- hdu2609(最小表示法+set)
- hdu2609---How many
- hdu2609 字符串的最小表示法模版
- HDU2609(字符串的最小表示法)
- How many-字典树+最小表示法
- hdu 2609 How many(最小表示法)
- [最小表示法] hdu 2609 How many
- [最小表示法] HDU 2609 How many
- HDU - 2609 How many(最小表示法)
- HDU 2609 How many(最小表示法)
- MongoDB:Too many open files
- 快速掌握Lua 5.3 —— 扩展你的程序 (1)
- Java高级开发之反射(一)
- 【C++】编程小结① -- new和delete
- LVS:三种负载均衡方式比较
- 最小表示法+hash hdu2609 How many
- Path类的方法笔记
- ubuntu系统 有线网络无法通过网页认证上网
- 蓝桥杯——李白打酒
- 蓝桥杯 曼哈顿距离
- Codeforces 28B pSort(dfs,判断连通)
- lvs、haproxy、nginx 负载均衡的比较分析
- hdu 1532 Drainage Ditches(EK算法)
- 【vijos1066】弱弱的战壕