SP7586 NUMOFPAL - Number of Palindromes(回文樹)
阿新 • • 發佈:2018-06-30
rate ren end different out node code clas 長度
題意翻譯
求一個串中包含幾個回文串
題目描述
Each palindrome can be always created from the other palindromes, if a single character is also a palindrome. For example, the string "malayalam" can be created by some ways:
- malayalam = m + ala + y + ala + m
- malayalam = m + a + l + aya + l + a + m
We want to take the value of function NumPal(s) which is the number of different palindromes that can be created using the string S by the above method. If the same palindrome occurs more than once then all of them should be counted separately.
輸入輸出格式
輸入格式:
The string S.
輸出格式:
The value of function NumPal(s).
輸入輸出樣例
輸入樣例#1: 復制
malayalam
輸出樣例#1: 復制
15
說明
Limitations
0 < |s| <= 1000
題解
這不是一道傻逼模板題嗎?
我們只需要好好利用一下cnt數組,統計一下len不為1的回文串。
然後再加上一個|s|的長度就好了。
代碼
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<iostream>
using namespace std;
struct node{
int fail,len,ch[26],cnt;
}t[1000010];
char s[1000100];
int n,ans;
void solve()
{
int k=0,tot=1;t[0].fail=t[1].fail=1;t[1].len=-1;s[0]='$';
for(int i=1;i<=n;i++)
{
while(s[i-t[k].len-1]!=s[i])k=t[k].fail;
if(!t[k].ch[s[i]-'a' ]){
t[++tot].len=t[k].len+2;
int j=t[k].fail;
while(s[i-t[j].len-1]!=s[i])j=t[j].fail;
t[tot].fail=t[j].ch[s[i]-'a'];
t[k].ch[s[i]-'a']=tot;
}
k=t[k].ch[s[i]-'a'];
t[k].cnt++;
}
for(int i=tot;i>=2;i--)
t[t[i].fail].cnt+=t[i].cnt;
for(int i=tot;i>=2;i--)
if(t[i].len!=1)
ans+=t[i].cnt;
}
int main()
{
scanf("%s",s+1);
n=strlen(s+1);
solve();
cout<<ans+n<<endl;
}
SP7586 NUMOFPAL - Number of Palindromes(回文樹)