Finding Palindromes
Time Limit: 10000MS | Memory Limit: 262144K | |
Total Submissions: 3093 | Accepted: 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
a a a ba ab a ab ba ba ab
/************************************************************************* > 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/