bzoj1524 "POI2006" Pal

Source: Internet
Author: User

1524: [Poi2006]pal time limit: 5 Sec Memory Limit: 357 MB
Submit: 367 Solved: 121
[Submit] [Status] [Discuss] Description give n palindrome string s1, s2, ..., SN to find the following two tuples (I, J) the number of Si + SJ is still a palindrome string size input string total length not more than 2M bytes inputthe first line of input file Conta Ins 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 中文版 alphabet. You can assume this total length of all strings would not exceed 2,000,000. The different line strings is the same. Outputprint out only one integer, the number of palindromessample Input6
2 AA
3 ABA
3 AAA
6 Abaaba
5 AAAAA
4 ABBA

Sample Output14





Trie + Hash just screw it up.

#SXOI2016之蜜汁错误 # In the examination room forget each string is palindrome string, want to complicate, finally only took 30, left 70 all tle.




#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <cstring > #include <algorithm> #define F (I,j,n) for (int. i=j;i<=n;i++) #define D (i,j,n) for (int i=j;i>=n;i--) # Define ll long long#define ull unsigned long long#define maxn 2000005#define base 233using namespace Std;int n,tot=1;int L [Maxn],cnt[maxn],num[maxn],t[maxn][26];ll ans;ull ha[maxn],p[maxn];char ch[maxn];string s[maxn];inline int read () { int X=0,f=1;char Ch=getchar (); while (ch< ' 0 ' | | Ch> ' 9 ') {if (ch== '-') F=-1;ch=getchar ();} while (ch>= ' 0 ' &&ch<= ' 9 ') {x=x*10+ch-' 0 '; Ch=getchar ();} return x*f;} int main () {p[0]=1; F (i,1,2000000) p[i]=p[i-1]*base;n=read (); F (i,1,n) {l[i]=read (); scanf ("%s", ch); s[i]=ch;//string so read in faster//cin>>s[i];//tleint now=1;ull tmp=0; F (j,0,l[i]-1) {int x=s[i][j]-' a '; if (!t[now][x]) t[now][x]=++tot;now=t[now][x];tmp=tmp*base+ (ull) (x+1);} Cnt[now]++;num[now]=i;ha[i]=tmp;} F (i,1,n) {int now=1; F (j,0,l[i]-1) {now=t[now][s[i][j]-' a '];if (cnt[now]&&amP;ha[num[now]]*p[l[i]]+ha[i]==ha[i]*p[l[num[now]]]+ha[num[now]]) Ans+=cnt[now];}} ans=ans*2-n;printf ("%lld\n", ans); return 0;}


bzoj1524 "POI2006" Pal

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.