POJ 3376 Finding Palindromes

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

内容概要:《2024年中国城市低空经济发展指数报告》由36氪研究院发布,指出低空经济作为新质生产力的代表,已成为中国经济新的增长点。报告从发展环境、资金投入、创新能力、基础支撑和发展成效五个维度构建了综合指数评价体系,评估了全国重点城市的低空经济发展状况。北京和深圳在总指数中名列前茅,分别以91.26和84.53的得分领先,展现出强大的资金投入、创新能力和基础支撑。低空经济主要涉及无人机、eVTOL(电动垂直起降飞行器)和直升机等产品,广泛应用于农业、物流、交通、应急救援等领域。政策支持、市场需求和技术进步共同推动了低空经济的快速发展,预计到2026年市场规模将突破万亿元。 适用人群:对低空经济发展感兴趣的政策制定者、投资者、企业和研究人员。 使用场景及目标:①了解低空经济的定义、分类和发展驱动力;②掌握低空经济的主要应用场景和市场规模预测;③评估各城市在低空经济发展中的表现和潜力;④为政策制定、投资决策和企业发展提供参考依据。 其他说明:报告强调了政策监管、产业生态建设和区域融合错位的重要性,提出了加强法律法规建设、人才储备和基础设施建设等建议。低空经济正加速向网络化、智能化、规模化和集聚化方向发展,各地应找准自身比较优势,实现差异化发展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值