zoukankan      html  css  js  c++  java
  • poj3376Finding Palindromes

    题解: 我们考虑两个串合成一个回文串 当且仅当翻转一个串后 另外一个串是这个串的前缀部分且后半部分是回文 即可完成配对 那么我们考虑处理出每个串的反串的每个位置是否能够成回文 然后插入trie树中 打上标记 然后对于每个串在trie上分大于当前和小于当前长度来考虑贡献即可

    #include <algorithm>
    #include <iostream>
    #include <cstring>
    #include <cstdio>
    const int MAXN=2e6+10;
    #define ll long long
    using namespace std;
    int nxt[MAXN],c[MAXN],pre[MAXN],last[MAXN];
    char str[MAXN],s1[MAXN],s2[MAXN];
    void ex_next(int ex_Nxt[],char a[]){
        int len=strlen(a);
        ex_Nxt[0]=len;
        int id_max=1;
        int t=0;
        while(t<len-1&&a[t]==a[t+1]) t++;
        ex_Nxt[1]=t;
        for(int k=2;k<len;k++){
            int p=ex_Nxt[id_max]+id_max-1;int l=ex_Nxt[k-id_max];
            if(l+k-1>=p){
                int j=(p-k+1)>0?p-k+1:0;
                while(j+k<len&&a[j]==a[j+k]) j++;
                ex_Nxt[k]=j;
                id_max=k;
            }
            else ex_Nxt[k]=l;
        }
        return ;
    }
    void ex_exid(int ex_excd[],int ex_Next[],char a[],char b[]){
        int len=strlen(a);
        int max_id=0;
        int t=0;
        int len1=strlen(b);
        int len2=min(len1,len);
        while(t<len2&&a[t]==b[t]) t++;
        ex_excd[0]=t;
        for(int i=1;i<len;i++){
            int p=ex_excd[max_id]+max_id-1;int l=ex_Next[i-max_id];
            if(i+l-1>=p){
                int j=(p-i+1)>0?p-i+1:0;
                while(j+i<len&&j<len1&&a[j+i]==b[j]) j++;
                ex_excd[i]=j;
                max_id=i;
            }
            else ex_excd[i]=l;
        }
        return ;
    }
    int cnt,rt;
    typedef struct node{
        int a[26];ll sum;int flag;
    }node;
    node d[MAXN];
    void newnode(int &x){
        x=++cnt;d[x].sum=0;d[x].flag=0;
        for(int i=0;i<26;i++)d[x].a[i]=0;
    }
    void insert(char s[]){
        int len=strlen(s);int temp=rt;
        for(int i=0;i<len;i++){
            int t=s[i]-'a';
            if(!d[temp].a[t])newnode(d[temp].a[t]);
            d[temp].sum+=c[i];
            temp=d[temp].a[t];
        }
        d[temp].sum++;
        d[temp].flag++;
    }
    ll querty(char s[]){
        int len=strlen(s);int temp=rt;ll res=0;
        for(int i=0;i<len;i++){
            int t=s[i]-'a';
            if(!d[temp].a[t])return res;
            temp=d[temp].a[t];
            if(i<len-1&&c[i+1]&&d[temp].flag)res+=d[temp].flag;
        }
        return d[temp].sum+res;
    }
    int main(){
        int n;
        while(scanf("%d",&n)!=EOF){
            int t;cnt=0;newnode(rt);last[0]=-1;
            for(int i=1;i<=n;i++){
                pre[i]=last[i-1]+1;
                scanf("%d%s",&t,str+pre[i]);
                last[i]=pre[i]+t-1;
            }
            for(int i=1;i<=n;i++){
                for(int j=pre[i];j<=last[i];j++)s1[j-pre[i]]=s2[j-pre[i]]=str[j];
                s1[last[i]-pre[i]+1]='';s2[last[i]-pre[i]+1]='';
                t=strlen(s1);
                reverse(s1,s1+t);
                ex_next(nxt,s2);
                ex_exid(c,nxt,s1,s2);
                for(int j=0;j<t;j++){
                    if(c[j]==t-j)c[j]=1;
                    else c[j]=0;
                }
                insert(s1);
            }
            ll ans=0;
            for(int i=1;i<=n;i++){
                for(int j=pre[i];j<=last[i];j++)s1[j-pre[i]]=s2[j-pre[i]]=str[j];
                s1[last[i]-pre[i]+1]='';s2[last[i]-pre[i]+1]='';
                t=strlen(s1);
                reverse(s2,s2+t);
                ex_next(nxt,s2);
                ex_exid(c,nxt,s1,s2);           
                for(int j=0;j<t;j++){
                    if(c[j]==t-j)c[j]=1;
                    else c[j]=0;
                }
                ans+=querty(s1);
            }
            printf("%lld
    ",ans);
        }
        return 0;
    }
    
                                                        Finding Palindromes
    Time Limit: 10000MS   Memory Limit: 262144K
    Total Submissions: 4654   Accepted: 872
    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

  • 相关阅读:
    对象属性对话框只能放大不能缩小
    Windows2003 Server升级至SP2后,水晶报表导出EXCEL出错
    磁盘结构损坏且无法读取
    InsusCharacterUtility.dll怎么调用
    会员综合实例
    无法连接SQL Server 2008
    JavaScript alert Utility
    DNS与Active Directory在两台服务器分别布署
    显示用户权限
    焦点新闻图片轮翻广告
  • 原文地址:https://www.cnblogs.com/wang9897/p/9818218.html
Copyright © 2011-2022 走看看