POJ 3376 Finding Palindromes

Finding Palindromes

Time Limit: 10000MSMemory Limit: 262144K
Total Submissions: 3093Accepted: 545
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:
a a a ba ab a ab ba ba ab
题意是给定如干个字符串,将其两两组合,有n × n种组合方式,求有多少个回文组合。
略难。。。
参考了别人的代码。运用字典树和扩展KMP
判断两个串的组合是不是回文串的方法有一种巧解,先举个例子。
比方说,adc 和 efecda这两个字符串顺序相连,显然会组成一个回文串。为什么会这样呢,因为efecda的反串adcefe的前缀adc与另外一个串匹配,并且剩下的efe是个回文串。efe如何判断是否是回文串呢,我们只需要一开始预处理将反串和串进行扩展KMP,用原串作为模板串,如果有extend[i] = len - i + 1的话,显然有[1..extend[i]]这段是回文的。
那么剩下需要解决的问题就是如何找到与回文串之后的子串匹配的串了。用字典树来解决这个问题。依次从后往前遍历每一个串,如果该串所对应字符在字典树中对应层数出现的话,那么只要满足这个字符之前的串是一个子串就可确定可与另外一个串组成回文串,这样的回文串有多少个呢,就是该层该字符对应的作为叶子的个数。
还有一种情况,倘若我们反向遍历完整个字符串,都有匹配,那么我们知道肯定存在至少一个字符串的前缀与该串匹配,我们只需要找到那些字符串的前缀之后的子串是回文串的那些即可,如何判断呢?拿反串作为模板串扩展KMP原串,还是上面的方法即可。
代码如下:
/*************************************************************************
	> File Name: Finding_Palindromes.cpp
	> Author: ZhangHaoRan
	> Mail: chilumanxi@gmail.com
	> Created Time: 2016年03月05日 星期五 8时30分43秒
 ************************************************************************/

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<queue>
#include<list>
#include<algorithm>

using namespace std;

const int MAXN = 2000010;
int n, templ, num;
int bg[MAXN], ed[MAXN];
char S[MAXN];
char R[MAXN];
bool a[2][MAXN];

struct trie{
    int son[26];
    int leaf;
    int cnt;
    void init(){
        for(int i = 0; i < 26; i ++)
            son[i] = 0;
        leaf = 0;
        cnt = 0;
    }
}tree[MAXN];

int nexti[MAXN];
int extend[MAXN];

void EKMP(char s[], char r[], int lens, int flag, int x){
    int j = 0;
    while(j + 2 < lens && r[j + 1] == r[j + 2])
        j ++;
    nexti[2] = j;
    int k = 2;
    for(int i = 3; i <= lens; i ++){
        int p = k + nexti[k] - 1;
        int l = nexti[i - k + 1];
        if(i + l < p + 1){
            nexti[i] = l;
        }
        else{
            j = max(0, p - i + 1);
            while(i + j <= lens && r[i + j] == r[j + 1])
                j ++;
            nexti[i] = j;
            k = i;
        }
    }

    j = 0;
    while(j <= lens && s[j] == r[j])
        j ++;
    extend[1] = j;
    k = 1;
    for(int i = 2; i <= lens; i ++){
        int p = k + extend[k] - 1;
        int l = nexti[i - k + 1];
        if(i + l < p + 1){
            extend[i] = l;
        }
        else{
            j = max(0, p - i + 1);
            while(i + j <= lens && s[i + j] == r[j + 1])
                j ++;
            extend[i] = j;
            k = i;
        }
    }

    for(int i = 1; i <= lens; i ++)
        if(extend[i] == lens - i + 1)
            a[flag][x + i] = true;
}


void insert(int x){
    int rt = 0;
    for(int i = bg[x]; i <= ed[x]; i ++){
        tree[rt].cnt += a[0][i];
        int ch = S[i] - 'a';
        if(!tree[rt].son[ch]){
            tree[rt].son[ch] = ++num;
            tree[num].init();
        }
        rt = tree[rt].son[ch];
    }
    ++ tree[rt].leaf;
}
int main(void){
    while(~scanf("%d", &n)){
        memset(a, 0, sizeof(a));
        templ = num = 0;
        tree[0].init();
        int len;
        for(int i = 0; i < n; i ++){
            scanf("%d%s", &len, S + templ + 1);
            for(int j = 1; j <= len; j ++){
                R[templ + j] = S[templ + len - j + 1];
            }
            R[templ + len + 1] = 0;
            EKMP(S + templ, R + templ, len, 0, templ);
            EKMP(R + templ, S + templ, len, 1, templ);
            bg[i] = templ + 1;
            templ += len;
            ed[i] = templ;
            insert(i);
        }

        long long ans = 0;
        for(int i = 0; i < n; i ++){
            int rt = 0;
            for(int j = ed[i]; j >= bg[i]; j --){
                rt = tree[rt].son[S[j] - 'a'];
                if(!rt)
                    break;
                if(a[1][ed[i] - j + bg[i] + 1] || j == bg[i])
                    ans += tree[rt].leaf;
            }
            if(rt)
                ans += tree[rt].cnt;
        }
        cout << ans << endl;
    }
    return 0;
}

 

查看原文:http://chilumanxi.org/2016/03/05/poj-3376-finding-palindromes/

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值