求回文子串个数(待补充)

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define MAXN 5005
char str[MAXN];

int main()
{
//    freopen("C:\Users\Sky\Desktop\1.in","r",stdin);//PLEASE DELETE IT!!!!!!!!!!!!!!!!!!!!!!!!
    while(~scanf("%s",&str))
    {
        int len=strlen(str),l,r;
        int ans=len;
        for(int i=0; i<len; i++)
        {
            l=i-1,r=i+1;//奇数长度
            while(l>=0&&r<len&&str[l]==str[r])
            {
                l--,r++;
                ans++;
            }
            l=i,r=i+1;//偶数长度
            while(l>=0&&r<len&&str[l]==str[r])
            {
                l--,r++;
                ans++;
            }
        }
        printf("%d
",ans);
    }
    return 0;
}
View Code

先贴个枚举版,还有后缀数组版,弄明白再贴

原文地址:https://www.cnblogs.com/Skyxj/p/3272889.html