HDU - 6230 Palindrome

Alice like strings, especially long strings. For each string, she has a special evaluation system to judge how elegant the string is. She defines that a string S[1..3n−2](n≥2)S[1..3n−2](n≥2) is one-and-half palindromic if and only if it satisfies S[i]=S[2n−i]=S[2n+i−2](1≤i≤n)S[i]=S[2n−i]=S[2n+i−2](1≤i≤n).For example, abcbabcabcbabc is one-and-half palindromic string, and abccbaabcabccbaabc is not. Now, Alice has generated some long strings. She ask for your help to find how many substrings which is one-and-half palindromic.

Input

The first line is the number of test cases. For each test case, there is only one line containing a string(the length of strings is less than or equal to 500000500000), this string only consists of lowercase letters.

Output

For each test case, output a integer donating the number of one-and-half palindromic substrings.

Sample Input

1
ababcbabccbaabc

Sample Output

2

        
  

Hint

In the example input, there are two substrings which are one-and-half palindromic strings, $abab$ and $abcbabc$.

 

类似于回文串的一种半回文串。

这道题要用马拉车算法,很遗憾我不会;要用树状数组,很遗憾我也不会。所以……

AC代码:

#include <cstring>
#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;

const int maxn=500010;
typedef long long LL;

vector <int> ve[maxn];
int a[maxn],n;
char s[maxn];
int Len[maxn],tlen[maxn];
int Manacher(char *st,int len)
{
    int mx=0,ans=0,po=0;
    for(int i=1;i<=len;i++)
    {
        if(mx>i)
            Len[i]=min(mx-i,Len[2*po-i]);
        else
            Len[i]=1;
        while(st[i-Len[i]]==st[i+Len[i]])
            Len[i]++;
        if(Len[i]+i>mx)
        {
            mx=Len[i]+i;
            po=i;
        }
        if(ans<Len[i])
            ans=Len[i];
    }
    return ans-1;
}

int lowbit(int x)
{
    return x&(-x);
}

int add(int pos,int v)
{
    while(pos<=n)
    {
        a[pos]+=v;
        pos+=lowbit(pos);
    }
}

int query(int pos)
{
    int ans=0;
    while(pos)
    {
        ans+=a[pos];
        pos-=lowbit(pos);
    }
    return ans;
}

int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        memset(a,0,sizeof(a));
        for(int i=0;i<maxn;i++)
            ve[i].clear();
        memset(s,0,sizeof(s));
        scanf("%s",s+1);
        int l=strlen(s+1);
        s[0]='$';
        Manacher(s,l);
        for(int i=2;i<=l;i++)
        {
            tlen[i]=Len[i]-1;
            ve[i-tlen[i]].push_back(i);
        }

        LL ans=0;n=l;
        for(int i=1;i<=n;i++)
        {
            for(int j=0;j<ve[i].size();j++)
                add(ve[i][j],1);
            ans+=query(min(i+tlen[i],n))-query(i);
        }
        cout<<ans<<endl;
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值