前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >SPOJ Number of Palindromes(回文树)

SPOJ Number of Palindromes(回文树)

作者头像
ShenduCC
发布2018-04-26 17:26:18
5540
发布2018-04-26 17:26:18
举报
文章被收录于专栏:算法修养算法修养

Number of Palindromes

Time Limit: 100MS

Memory Limit: 1572864KB

64bit IO Format: %lld & %llu

Submit Status

Description

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.

Input

The string S.

Output

The value of function NumPal(s).

Limitations

0 < |s| <= 1000

Example

Input:

malayalam

Output:

15

Hint

Added by:

The quick brown fox jumps over the lazy dog

Date:

2010-10-18

Time limit:

0.100s-0.170s

Source limit:

50000B

Memory limit:

1536MB

Cluster:

Cube (Intel G860)

Languages:

All

Resource:

Udit Agarwal

回文树:

参考大牛的博客:http://blog.csdn.net/lwfcgz/article/details/48739051

回文树是一种处理回文串的强大工具

代码语言:javascript
复制
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <math.h>
#include <stdio.h>

using namespace std;
#define MAX 1005
struct Node
{
    int next[26];
    int len;
    int sufflink;
    int num;
}tree[MAX];
char s[MAX];

int num;
int suff;
bool addLetter(int pos)
{
    int cur=suff,curlen=0;
    int let=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[let])
    {
        suff=tree[cur].next[let];
        return false;
    }
    num++;
    suff=num;
    tree[num].len=tree[cur].len+2;
    tree[cur].next[let]=num;
    if(tree[num].len==1)
    {
        tree[num].sufflink=2;
        tree[num].num=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[num].sufflink=tree[cur].next[let];
            break;
        }
    }
    tree[num].num=1+tree[tree[num].sufflink].num;
    return true;

}
void initTree()
{
    num=2;suff=2;
    tree[1].len=-1;tree[1].sufflink=1;
    tree[2].len=0;tree[2].sufflink=1;
}
int main()
{
    scanf("%s",s);
    int len=strlen(s);
    initTree();
    long long int ans=0;
    for(int i=0;i<len;i++)
    {
         addLetter(i);
         ans+=tree[suff].num;
    }
    printf("%d\n",ans);
    return 0;
}

Number of Palindromes

Time Limit: 100MS

Memory Limit: 1572864KB

64bit IO Format: %lld & %llu

Submit Status

Description

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.

Input

The string S.

Output

The value of function NumPal(s).

Limitations

0 < |s| <= 1000

Example

Input:

malayalam

Output:

15

Hint

Added by:

The quick brown fox jumps over the lazy dog

Date:

2010-10-18

Time limit:

0.100s-0.170s

Source limit:

50000B

Memory limit:

1536MB

Cluster:

Cube (Intel G860)

Languages:

All

Resource:

Udit Agarwal

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016-05-07 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档