HDU 5157 Harry and magic string (BestCoder Round #25 D) Manacher(或 Palindromic Tree) + 前缀和
来源:互联网 发布:天津职业大学赛尔网络 编辑:程序博客网 时间:2024/06/03 12:55
题目大意:
就是现在给出一个字符串s, 长度不超过 10^5, 然后求出其中不相交的回文字串的对数
大致思路:
其实一眼看去就知道可以用Manacher处理出回文半径之后用前缀和解决 不过有想了一下Palindromic Tree的做法, 算是练习一下Palindromic Tree了
解法一:
Manacher处理出所有位置的回文半径然后计算以i位置结尾的回文串数量和以i位置开始的回文串数量, 然后其中一组乘上另外一组的前缀和加起来就是答案了
代码如下:
Result : Accepted Memory : 6016 KB Time : 109 ms
/* * Author: Gatevin * Created Time: 2015/3/31 10:03:33 * File Name: Rin_Tohsaka.cpp */#include<iostream>#include<sstream>#include<fstream>#include<vector>#include<list>#include<deque>#include<queue>#include<stack>#include<map>#include<set>#include<bitset>#include<algorithm>#include<cstdio>#include<cstdlib>#include<cstring>#include<cctype>#include<cmath>#include<ctime>#include<iomanip>using namespace std;const double eps(1e-8);typedef long long lint;#define maxn 100010char in[maxn];char s[maxn << 1];int R[maxn << 1];lint dp1[maxn], dp2[maxn], s1[maxn], s2[maxn];void Manacher(char *s, int *R, int n){ int mx = 0, p = 0; R[0] = 1; for(int i = 1; i <= n; i++) { if(mx > i) R[i] = min(R[2*p - i], mx - i); else R[i] = 1; while(s[i - R[i]] == s[i + R[i]]) R[i]++; if(i + R[i] > mx) mx = i + R[i], p = i; } return;}int main(){ while(scanf("%s", in) != EOF) { s[0] = '@'; int n = strlen(in); for(int i = 0; i < n; i++) s[2*i + 1] = in[i], s[2*i + 2] = '#'; s[2*n] = '$'; Manacher(s, R, 2*n); memset(dp1, 0, sizeof(dp1)); memset(dp2, 0, sizeof(dp2)); for(int i = 1; i < 2*n; i++) { int l = i - R[i] + 1, r = i; l >>= 1; r = (r & 1) ? r >> 1 : (r >> 1) - 1; if(l <= r) dp1[l]++, dp1[r + 1]--; l = i; r = i + R[i] - 1; l >>= 1; r = (r & 1) ? r >> 1 : (r >> 1) - 1; if(l <= r) dp2[l]++, dp2[r + 1]--; } //用s1[i]表示以第i个字符开头的回文串数量 //用s2[i]表示以第i个字符结尾的回文串数量 s1[0] = dp1[0], s2[0] = dp2[0]; for(int i = 1; i < n; i++) s1[i] = s1[i - 1] + dp1[i], s2[i] = s2[i - 1] + dp2[i]; //滚动数组用dp1表示s1的后缀和 dp1[n - 1] = s1[n - 1]; for(int i = n - 2; i >= 0; i--) dp1[i] = dp1[i + 1] + s1[i]; lint ans = 0; for(int i = 0; i < n - 1; i++) ans += s2[i]*dp1[i + 1]; printf("%I64d\n", ans); } return 0;}
解法二:
用Palindromic Tree 来计算上面那种方法中的s1, s2 (就是把串s插入树中两次, 一次正序一次倒序就行了, 计数思想一样), 只是得到s1, s2的手段不同罢了
代码如下:
Result : Accepted Memory : 14048 KB Time : 109 ms
/* * Author: Gatevin * Created Time: 2015/3/31 10:34:41 * File Name: Rin_Tohsaka.cpp */#include<iostream>#include<sstream>#include<fstream>#include<vector>#include<list>#include<deque>#include<queue>#include<stack>#include<map>#include<set>#include<bitset>#include<algorithm>#include<cstdio>#include<cstdlib>#include<cstring>#include<cctype>#include<cmath>#include<ctime>#include<iomanip>using namespace std;const double eps(1e-8);typedef long long lint;#define maxn 100010char s[maxn];lint dp[maxn];struct Palindromic_Tree{ struct node { int next[26]; int len; int sufflink; int cnt; }; node tree[maxn]; int L, len, suff; void newnode() { L++; for(int i = 0; i < 26; i++) tree[L].next[i] = -1; tree[L].len = tree[L].sufflink = tree[L].cnt = 0; return; } void init() { L = 0, suff = 2; newnode(), newnode(); tree[1].len = -1; tree[1].sufflink = 1; tree[2].len = 0; tree[2].sufflink = 1; return; } bool addLetter(int pos) { int cur = suff, curlen = 0; int alp = s[pos] - 'a'; while(1) { curlen = tree[cur].len; if(pos - 1 - curlen >= 0 && s[pos - 1 - curlen] == s[pos]) break; cur = tree[cur].sufflink; } if(tree[cur].next[alp] != -1) { suff = tree[cur].next[alp]; return false; } newnode(); suff = L; tree[L].len = tree[cur].len + 2; tree[cur].next[alp] = L; if(tree[L].len == 1) { tree[L].sufflink = 2; tree[L].cnt = 1; return true; } while(1) { cur = tree[cur].sufflink; curlen = tree[cur].len; if(pos - 1 - curlen >= 0 && s[pos - 1 - curlen] == s[pos]) { tree[L].sufflink = tree[cur].next[alp]; break; } } tree[L].cnt = 1 + tree[tree[L].sufflink].cnt; return true; }};Palindromic_Tree pal;int main(){ while(scanf("%s", s) != EOF) { pal.init(); int n = strlen(s); for(int i = 0; i < n; i++) { pal.addLetter(i); dp[i] = pal.tree[pal.suff].cnt;//dp[i]表示以i位置结尾的回文串的数量 } for(int i = 1; i < n; i++) dp[i] += dp[i - 1];//现在dp[i]表示在i位置或之前结尾的回文串数量 reverse(s, s + n); pal.init(); lint ans = 0; for(int i = 0; i < n; i++) { pal.addLetter(i); ans += pal.tree[pal.suff].cnt*dp[n - 1 - i - 1]; } printf("%I64d\n", ans); } return 0;}
0 0
- HDU 5157 Harry and magic string (BestCoder Round #25 D) Manacher(或 Palindromic Tree) + 前缀和
- hdu 5157 Harry and magic string(前缀和,manacher)
- 【HDU】5157 Harry and magic string 【Palindromic Tree】
- HDU 5157 Harry and magic string 【Palindromic Tree】
- HDU 5157 Harry and magic string(Manacher)
- hdu 5157 Harry and magic string(manacher+dp)
- hdu5157 Harry and magic string Manacher算法,差分前缀和
- [BestCoder Round #25 1002]Harry And Magic Box 解题报告
- HDU 5157 Harry and magic string
- [BestCoder Round #25 1003]Harry and Christmas tree
- [BestCoder] Round #25 1001 - Harry and Magical Computer | HDU 5154
- hdu 5154 Harry and Magical Computer(BestCoder Round #25)
- HDU 5157 Harry and magic string(回文树)
- HDU 5069 Harry And Biological Teacher AC自动机 + 线段树优化 (BestCoder Round 14 D)
- hdu 5068 Harry And Math Teacher(BestCoder Round #14)
- hdu 5066 Harry And Physical Teacher(Bestcoder Round #14)
- hdu 5067 Harry And Dig Machine(BestCoder Round #14)
- HDU5157-Harry and magic string
- Gson解析Json 四
- Java NIO原理图文分析及代码实现
- dev-C++调试崩溃问题
- Factorial Trailing Zeroes
- error LNK1123: 转换到 COFF 期间失败: 文件无效或损坏
- HDU 5157 Harry and magic string (BestCoder Round #25 D) Manacher(或 Palindromic Tree) + 前缀和
- c# 类
- J2EE基础总结(1)——J2EE入门
- android 开发 多屏适配
- Machine Learning Classical Books Intro - 02
- socket的半包,粘包与分包的问题
- wnd32TEXTMETRIC字体结构
- java对象版本序列化
- 三层架构 C# 实现篇