HDU 5157 Harry and magic string 【Palindromic Tree】
来源:互联网 发布:hid门禁软件 编辑:程序博客网 时间:2024/05/21 08:57
回文树第一题,太裸了。
题目分析:
令len为字符串长度,字符串下标从0开始。从字符串最后一个字符开始插入回文树构造回文树,记录以下标i~len-1为起点的回文串的个数的sum[i]。然后再正着构造一次回文树,边构造边累加答案,设x为以下标i的字符为结尾的回文串个数,则ans+=x*sum[i+1]。
最后输出ans即可。
PS:如果想了解回文树,可以戳这里:Palindromic Tree——回文树【处理一类回文串问题的强力工具】
// whn6325689// Mr.Phoebe// http://blog.csdn.net/u013007900#include <algorithm>#include <iostream>#include <iomanip>#include <cstring>#include <climits>#include <complex>#include <fstream>#include <cassert>#include <cstdio>#include <bitset>#include <vector>#include <deque>#include <queue>#include <stack>#include <ctime>#include <set>#include <map>#include <cmath>#include <functional>#include <numeric>#pragma comment(linker, "/STACK:1024000000,1024000000")using namespace std;#define eps 1e-9#define PI acos(-1.0)#define INF 0x3f3f3f3f#define LLINF 1LL<<62#define speed std::ios::sync_with_stdio(false);typedef long long ll;typedef unsigned long long ull;typedef long double ld;typedef pair<ll, ll> pll;typedef complex<ld> point;typedef pair<int, int> pii;typedef pair<pii, int> piii;typedef vector<int> vi;#define CLR(x,y) memset(x,y,sizeof(x))#define CPY(x,y) memcpy(x,y,sizeof(x))#define clr(a,x,size) memset(a,x,sizeof(a[0])*(size))#define cpy(a,x,size) memcpy(a,x,sizeof(a[0])*(size))#define mp(x,y) make_pair(x,y)#define pb(x) push_back(x)#define lowbit(x) (x&(-x))#define MID(x,y) (x+((y-x)>>1))#define ls (idx<<1)#define rs (idx<<1|1)#define lson ls,l,mid#define rson rs,mid+1,r#define root 1,1,ntemplate<class T>inline bool read(T &n){ T x = 0, tmp = 1; char c = getchar(); while((c < '0' || c > '9') && c != '-' && c != EOF) c = getchar(); if(c == EOF) return false; if(c == '-') c = getchar(), tmp = -1; while(c >= '0' && c <= '9') x *= 10, x += (c - '0'),c = getchar(); n = x*tmp; return true;}template <class T>inline void write(T n){ if(n < 0) { putchar('-'); n = -n; } int len = 0,data[20]; while(n) { data[len++] = n%10; n /= 10; } if(!len) data[len++] = 0; while(len--) putchar(data[len]+48);}//-----------------------------------const int MAXN = 100010 ; const int N = 26 ; struct Palindromic_Tree { int next[MAXN][N] ;//next指针,next指针和字典树类似,指向的串为当前串两端加上同一个字符构成 int fail[MAXN] ;//fail指针,失配后跳转到fail指针指向的节点 int cnt[MAXN] ; int num[MAXN] ; int len[MAXN] ;//len[i]表示节点i表示的回文串的长度 int S[MAXN] ;//存放添加的字符 int last ;//指向上一个字符所在的节点,方便下一次add int n ;//字符数组指针 int p ;//节点指针 int newnode ( int l ) {//新建节点 for ( int i = 0 ; i < N ; ++ i ) next[p][i] = 0 ; cnt[p] = 0 ; num[p] = 0 ; len[p] = l ; return p ++ ; } void init () {//初始化 p = 0 ; newnode ( 0 ) ; newnode ( -1 ) ; last = 0 ; n = 0 ; S[n] = -1 ;//开头放一个字符集中没有的字符,减少特判 fail[0] = 1 ; } int get_fail ( int x ) {//和KMP一样,失配后找一个尽量最长的 while ( S[n - len[x] - 1] != S[n] ) x = fail[x] ; return x ; } int add ( int c ) { c -= 'a' ; S[++ n] = c ; int cur = get_fail ( last ) ;//通过上一个回文串找这个回文串的匹配位置 if ( !next[cur][c] ) {//如果这个回文串没有出现过,说明出现了一个新的本质不同的回文串 int now = newnode ( len[cur] + 2 ) ;//新建节点 fail[now] = next[get_fail ( fail[cur] )][c] ;//和AC自动机一样建立fail指针,以便失配后跳转 next[cur][c] = now ; num[now] = num[fail[now]] + 1 ; } last = next[cur][c] ; cnt[last] ++ ; return num[last]; } void count () { for ( int i = p - 1 ; i >= 0 ; -- i ) cnt[fail[i]] += cnt[i] ; //父亲累加儿子的cnt,因为如果fail[v]=u,则u一定是v的子回文串! } }T; char str[MAXN];ll sum[MAXN],ans;int main(){ while(~scanf("%s",str)) { T.init();CLR(sum,0);ans=0; int len=strlen(str); for(int i=len-1;i>=0;i--) sum[i]=sum[i+1]+T.add(str[i]); T.init(); for(int i=0;i<len;i++) ans+=sum[i+1]*T.add(str[i]); write(ans),putchar('\n'); } return 0;}
0 0
- 【HDU】5157 Harry and magic string 【Palindromic Tree】
- HDU 5157 Harry and magic string 【Palindromic Tree】
- 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
- hdu 5157 Harry and magic string(manacher+dp)
- HDU 5157 Harry and magic string(回文树)
- hdu 5157 Harry and magic string(前缀和,manacher)
- HDU5157-Harry and magic string
- hdu 5155 Harry And Magic Box(DP)
- hdu 5155 Harry And Magic Box
- HDU - 5155 Harry And Magic Box
- HDU 5155 Harry And Magic Box
- HDU 5155 Harry And Magic Box --DP
- Hdu 5155 Harry And Magic Box
- Harry And Magic Box
- 【HDU】5155 Harry And Magic Box 【容斥原理】
- HDU 5155 Harry And Magic Box DP题
- Java多线程总结笔记(三)—— 同步和锁机制、synchronized
- 大会插件
- 希尔排序算法和快速排序算法
- Html 让文字显示在图片的上面
- android环境配置
- HDU 5157 Harry and magic string 【Palindromic Tree】
- <五>java数据结构与算法 对象排序
- 学习笔记(九)并发(二)
- 字符串通配符
- 雪花模型,星型模型
- LeetCode Invert Binary Tree OJ 刷题 算法
- codeforces 570 D. Tree Requests 树状数组+dfs搜索序
- 【大数据处理架构】3. kafka安装与使用
- 浅谈HTTP中Get与Post的区别