UESTC 1703 一道更简单的字符串题 哈希+枚举
2017-07-18 21:56
337 查看
一道更简单的字符串题
Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others)Submit Status
有一天,潘爷在白纸上写了字符串A,然后他不断的重复写这个字符串A,于是白纸上出现了一个新的字符串:AAAAAAAA....。现在我从这个字符串的两个不同的位置截取了一段字符串叫做B,现在给你这个字符串B,你能告诉我潘爷可能写的最短的字符串A有多长吗。举个例子,A=“abcdefg”,我获得的字符串是 abcd efgabcdefgabcde fgabcdefg..... ,现在我把红色的部分“efgabcdefgabcde”作为B,你需要找到最短的符合条件的字符串A的长度。
Input
输入包括一个字符串B,长度不大于1000000;Output
对于每组数据,输出一个整数,代表最短的字符串A的长度。Sample input and output
Sample Input Sample Outputefgabcdefgabcde 7
Source
2017 UESTC Training for Search Algorithm & StringUESTC 1703 一道更简单的字符串题
My Solution
求出最小循环串。哈希+枚举
先把字符串哈希,
然后从ans = 1 ~ n进行枚举,
check的时候枚举每个hash(i, ans) == hash(0, ans),
且对于最后一段长度不到ans的 hash(i, len-i) == hash(0, len-i),
因为是从小到大枚举,所以一旦有ans满足,则就是答案。
这是一个调和级数,故nlin(n)。
时间复杂度 O(nlin(n))
空间复杂度 O(n)
#include <iostream> #include <cstdio> #include <cstring> using namespace std; typedef unsigned long long ull; const int MAXN = 1e6 + 8; const ull p = 1e12 + 7; //! struct HASH{ ull Hash[MAXN], xp[MAXN]; void init1(int sz){ xp[0] = 1; for(int i = 1; i <= sz; i++) xp[i] = xp[i-1] * p; } void init2(char *s){//0~n-1 based int sz = strlen(s); Hash[sz] = 0; for(int i = sz - 1; i >= 0; i--){ Hash[i] = Hash[i+1] * p + (s[i] - 'a' + 1); } } ull get_Hash(const int& st, const int& len){ return Hash[st] - Hash[st + len] * xp[len]; } } hh; char s[MAXN]; int len; inline bool check(int x){ for(int i = x; i < len; i += x){ if(len - i >= x){ if(hh.get_Hash(i, x) != hh.get_Hash(0, x)){ return false; } } else{ if(hh.get_Hash(i, len - i) != hh.get_Hash(0, len - i)){ return false; } } } return true; } int main() { #ifdef LOCAL freopen("d.txt", "r", stdin); //freopen("d.out", "w", stdout); int T = 4; while(T--){ #endif // LOCAL //ios::sync_with_stdio(false); cin.tie(0); scanf("%s", s); len = strlen(s); hh.init1(len); hh.init2(s); int ans = 0, i; for(i = 1; i <= len; i++){ if(check(i)){ ans = i; break; } } printf("%d\n", ans); #ifdef LOCAL cout << endl; } #endif // LOCAL return 0; }
Thank you!
------from ProLights
相关文章推荐
- UESTC 1691 这是一道比CCCC简单题经典的中档题 多重背包
- UESTC 1691 这是一道比CCCC简单题经典的中档题
- UESTC 1692 这是一道比CCCC简单题更有想象力的中档题 完全背包
- UESTC 1692 这是一道比CCCC简单题更有想象力的中档题
- 字符串转换成整数 - 解庞果英雄会的一道简单挑战题
- uestc L - 这是一道比CCCC简单题难的简单题
- 一道简单的面试题:字符串转换成整数(修正版)
- 一道简单的面试题:输入字符串,转换成整数输出。
- UESTC 1690 这是一道比CCCC简单题难的简单题
- 【字符串模拟ONE】2012年省赛最简单的一道PROBLEM_G
- UESTC 1690 这是一道比CCCC简单题难的简单题 状态压缩dp
- UESTC 1696 一道简单的字符串题 KMP+dp
- oracle trim函数与字符串替换的内存实现简单猜测
- 一道简单的机试题,考倒多少人?
- 一道简单的算法题:输入日期(JAVA)
- 简单的C语言字符串替换问题——
- 字符串简单加密算法
- vc简单加密字符串
- 简单字符串解压程序代码
- hdoj1039简单题、字符串处理