POJ 3376 Finding Palindromes

本文介绍了一个POJ 3376题目的解决方案,利用Trie树和Manacher算法来找出由给定字符串生成的所有可能配对中形成的回文串数量。

http://poj.org/problem?id=3376

Finding Palindromes
Time Limit: 10000MS Memory Limit: 262144K
Total Submissions: 3973 Accepted: 726
Case Time Limit: 2000MS

Description

A word is called a palindrome if we read from right to left is as same as we read from left to right. For example, "dad", "eye" and "racecar" are all palindromes, but "odd", "see" and "orange" are not palindromes.

Given n strings, you can generate n × n pairs of them and concatenate the pairs into single words. The task is to count how many of the so generated words are palindromes.

Input

The first line of input file contains the number of strings n. The following n lines describe each string:

The i+1-th line contains the length of the i-th string li, then a single space and a string of li small letters of English alphabet.

You can assume that the total length of all strings will not exceed 2,000,000. Two strings in different line may be the same.

Output

Print out only one integer, the number of palindromes.

Sample Input

3
1 a
2 ab
2 ba

Sample Output

5

Hint

The 5 palindromes are:
aa aba aba abba baab

思路,trie加manacher算法,将所有的字符串反向插入字典树中,记录结尾和字符串中回文串的个数,然后每个字符串通过manacher找到回文串的前后缀;

这样插入到字符串中时,前缀就变成了后缀,在查询时,相当于拿前缀查找反转后的前缀,相当于后缀,剩下的根据字符串的特性,如果中查找到就说明可以完全匹配;

再提交时,用指针写,一直时间超限,改为结构体数组后就可以过了。

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <vector>
using namespace std;
typedef long long ll;
int p[4000009],a[2000009];
char s[2000009],ss[4000009];
bool ft[2000006],lt[2000009];
int pos;
struct Trie
{
    int next[26];
    int leaf;
    int cnt;
    void init()
    {
        leaf=cnt=0;
        memset(next,false,sizeof(next));
    }
}trie[2000006];
void buildtrie(int n)
{
    int x=0;
    for(int i=a[n+1]-1;i>=a[n];i--)
    {
        int k=s[i]-'a';
        trie[x].cnt+=ft[i];//倒着插前缀变后缀
        if(trie[x].next[k]==0)
        {
            trie[x].next[k]=++pos;
            trie[pos].init();
        }
        x=trie[x].next[k];
    }
    trie[x].leaf+=1;
}
void manacher(int n)
{
    int id=0,l=1;
    ss[0]='@';
    for(int i=a[n];i<a[n+1];i++)
    {
        ss[l++]='#';
        ss[l++]=s[i];
        ft[i]=false;
        lt[i]=false;
    }
    ss[l]='#';
    ss[l+1]='0';
    p[0]=0;
    for(int i=2;i<l;i++)
    {
        p[i]=1;
        if(id+p[id]>i)p[i]=min(p[id*2-i],p[id]+id-i);
        while(ss[i+p[i]]==ss[i-p[i]]) p[i]++;
        if(p[id]+id<i+p[i]) id=i;
        if(p[i]==i) ft[a[n]+p[i]-2]=true;
        if(p[i]+i-1==l) lt[a[n+1]-p[i]+1]=true;
    }
}
int query(int n)
{
    int i,x=0,ans=0;
    for(i=a[n];i<a[n+1];i++)
    {
        int k=s[i]-'a';
        if(trie[x].next[k]==0)
            break;
        x=trie[x].next[k];
        if(lt[i+1] || i==a[n+1]-1)//匹配后缀
            ans+=trie[x].leaf;
    }
    if(i==a[n+1])
        ans+=trie[x].cnt;//完全匹配
    return ans;
}
int main()
{
    int t;
    scanf("%d",&t);
    pos=0;
    trie[0].init();
    int l;
    a[0]=0;
    for(int i=1;i<=t;i++)
    {
        scanf("%d%s",&l,s+a[i]);
        a[i+1]=a[i]+l;
        manacher(i);
        buildtrie(i);
    }
    ll ans=0;
    for(int i=1;i<=t;i++)
        ans+=query(i);
    printf("%lld\n",ans);
    return 0;
}

再来一个时间超限的指针代码

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <vector>
using namespace std;
typedef long long ll;
int p[4000009],a[2000009];
char s[2000009],ss[4000009];
bool ft[2000006],lt[2000009];
struct trie
{
    trie *next[26];
    int leaf;
    int cnt;
    trie()
    {
        leaf=cnt=0;
        for(int i=0;i<26;i++)
            next[i]=NULL;
    }
};
trie *root;
void buildtrie(int n)
{
    trie *p=root;
    trie *tmp;
    for(int i=a[n+1]-1;i>=a[n];i--)
    {
        int k=s[i]-'a';
        p->cnt+=ft[i];
        if(p->next[k]==NULL)
        {
            tmp=new trie();
            p->next[k]=tmp;
        }
        p=p->next[k];
    }
    p->leaf+=1;
}
void manacher(int n)
{
    int id=0,l=1;
    ss[0]='@';
    for(int i=a[n];i<a[n+1];i++)
    {
        ss[l++]='#';
        ss[l++]=s[i];
        ft[i]=false;
        lt[i]=false;
    }
    ss[l]='#';
    ss[l+1]='0';
    p[0]=0;
    for(int i=2;i<l;i++)
    {
        p[i]=(id+p[id]>i)?min(p[id*2-i],p[id]+id-i):1;
        while(ss[i+p[i]]==ss[i-p[i]]) p[i]++;
        if(p[id]+id<i+p[i]) id=i;
        if(p[i]==i) ft[a[n]+p[i]-2]=true;
        if(p[i]+i-1==l) lt[a[n+1]-p[i]+1]=true;
    }
}
int query(int n)
{
    trie *p=root;
    int i,ans=0;
    for(i=a[n];i<a[n+1];i++)
    {
        int k=s[i]-'a';
        if(p->next[k]==NULL)
            break;
        p=p->next[k];
        if(lt[i+1] || i==a[n+1]-1)
            ans+=p->leaf;
    }
    if(i==a[n+1])
        ans+=p->cnt;
    return ans;
}
int main()
{
    int t;
    scanf("%d",&t);
    int l;
    root=new trie();
    a[0]=0;
    for(int i=1;i<=t;i++)
    {
        scanf("%d%s",&l,s+a[i]);
        a[i+1]=a[i]+l;
        manacher(i);
        buildtrie(i);
    }
    ll ans=0;
    for(int i=1;i<=t;i++)
        ans+=query(i);
    printf("%lld\n",ans);
    return 0;
}
Time Limit Exceeded code

 

转载于:https://www.cnblogs.com/shinianhuanniyijuhaojiubujian/p/7144817.html

内容概要:本文系统介绍了算术优化算法(AOA)的基本原理、核心思想及Python实现方法,并通过图像分割的实际案例展示了其应用价值。AOA是一种基于种群的元启发式算法,其核心思想来源于四则运算,利用乘除运算进行全局勘探,加减运算进行局部开发,通过数学优化器加速函数(MOA)和数学优化概率(MOP)动态控制搜索过程,在全局探索与局部开发之间实现平衡。文章详细解析了算法的初始化、勘探与开发阶段的更新策略,并提供了完整的Python代码实现,结合Rastrigin函数进行测试验证。进一步地,以Flask框架搭建前后端分离系统,将AOA应用于图像分割任务,展示了其在实际工程中的可行性与高效性。最后,通过收敛速度、寻优精度等指标评估算法性能,并提出自适应参数调整、模型优化和并行计算等改进策略。; 适合人群:具备一定Python编程基础和优化算法基础知识的高校学生、科研人员及工程技术人员,尤其适合从事人工智能、图像处理、智能优化等领域的从业者;; 使用场景及目标:①理解元启发式算法的设计思想与实现机制;②掌握AOA在函数优化、图像分割等实际问题中的建模与求解方法;③学习如何将优化算法集成到Web系统中实现工程化应用;④为算法性能评估与改进提供实践参考; 阅读建议:建议读者结合代码逐行调试,深入理解算法流程中MOA与MOP的作用机制,尝试在不同测试函数上运行算法以观察性能差异,并可进一步扩展图像分割模块,引入更复杂的预处理或后处理技术以提升分割效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值