算法[串]
丶singularity
今夕复何夕
展开
-
POJ 2774 Long Long Message <后缀数组(DC3)>
题目:传送门。分析:求两个字符串的最长公共子串。后缀数组基础题,将一个链接在另一个后面,中间用一个不会出现的字符隔开。然后求height数组即可。遍历一遍height数组便可得到最长公共子串。代码:#include #include #include #include #include #include using namespace std;#defin原创 2017-04-28 15:12:23 · 161 阅读 · 0 评论 -
POJ 3693 Maximum Repetition Substring <后缀数组 + RMQ>
题目:传送门。分析:求连续重复次数最多的子串,若有多个,输出字典序最小的。感觉有点难度了,看了解题报告做的。代码:#include #include #include #include using namespace std;const int MAXN=100005;const int INF=0x3f3f3f3f;int wa[MAXN],wb[原创 2017-04-25 21:50:13 · 229 阅读 · 0 评论 -
POJ 2406 Power Strings <后缀数组(DC3) / KMP>
题目:传送门。分析:求字符串最短的循环节。用后缀数组的话,得用DC3算法,使用倍增算法会TLE。具体做法是,如果字符串存在长度为i的循环节,那么suffix(0)与suffix(i)的LCP=len-i,这是个充要条件,仔细想想就明白了。另外其实这道题用KMP应是更好地选择,具体做法是,如果如果len%(len-next[len])==0,则字符串存在长度为len-next[len]的原创 2017-04-24 22:39:21 · 146 阅读 · 0 评论 -
URAL 1297 Palindrome <后缀数组+RMQ>
题目:传送门分析:求最长回文子串。将原串倒置链接在后面,中间用一个不会出现的字符隔开(这样任何两个后缀的LCP都不会跨过它),再求height数组。代码:#include #include #include #include #include using namespace std;const int MAXN=2010;int sa[MAXN],wm[原创 2017-04-24 14:00:01 · 173 阅读 · 0 评论 -
SPOJ 694 Distinct Substrings <后缀数组>
题目:传送门分析:后缀数组水题。求有多少个不相同的子串,ans=n*(n+1)/2-sigma(height[1...n]).。代码:#include #include #include using namespace std;const int MAXN=1005;int sa[MAXN],wm[MAXN],wa[MAXN],wb[MAXN],heig原创 2017-04-23 23:37:05 · 162 阅读 · 0 评论 -
POJ 1743 Musical Theme <后缀数组+二分>
题目:传送门分析:求重复至少两次的无重叠子串,变化相同的也算一样,故可以先作差再求height数组。代码:#include #include using namespace std;const int MAXN=20010;int w[MAXN],wv[MAXN],wa[MAXN],wb[MAXN],sa[MAXN],data[MAXN],height[MAXN];int n;原创 2017-04-23 15:29:12 · 135 阅读 · 0 评论 -
POJ 3261 Milk Patterns <后缀数组+二分>
题目:传送门分析:水题,哈希也可以过。代码:#include #include using namespace std;const int MAXN=20010;struct node{ int x,id; bool operator <(node& N){ if(x!=N.x) return x<N.x; else return id<N.i原创 2017-04-23 18:20:08 · 141 阅读 · 0 评论 -
POJ 3294 Life Forms <后缀数组+二分>
题目:传送门。分析:求重复出现大于n/2次的最长子串,有多个时按字典序输出。对长度进行二分。RE了几次,注意满足条件的子串有可能有超过1000个。代码:#include #include #include #include #include #include using namespace std;const int MAXN=110;const i原创 2017-05-01 12:48:27 · 145 阅读 · 0 评论 -
POJ 3415 Common Substrings <后缀数组+单调栈>
题目:传送门。分析:求长度不小于k的公共子串一共有多少组。直接上代码吧。代码:#include #include #include #include #include using namespace std;const int MAXN=2e5+4;typedef long long LL;int wm[MAXN],wa[MAXN],wb[MAXN],原创 2017-04-28 22:12:04 · 124 阅读 · 0 评论 -
POJ 2503 Babelfish <串的哈希>
题目:传送门。分析:串的哈希基础题,采用ELFhash算法。另外这道题很水,用c++STL里的map写就更方便了。代码:#include #include #include #include #include using namespace std;const int MAXN=1e5+5;const int MOD=1e5+1;int cnt;in原创 2017-05-09 18:07:05 · 200 阅读 · 0 评论