本文主要是介绍HDU6230 Palindrome —— 马拉车+树状数组,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Alice like strings, especially long strings. For each string, she has a special evaluation system to judge how elegant the string is. She defines that a string S1…3n−2 is one-and-half palindromic if and only if it satisfies S[i]=S[2n−i]=S2n+i−2.For example, abcbabc is one-and-half palindromic string, and abccbaabc is not. Now, Alice has generated some long strings. She ask for your help to find how many substrings which is one-and-half palindromic.
Input
The first line is the number of test cases. For each test case, there is only one line containing a string(the length of strings is less than or equal to 500000), this string only consists of lowercase letters.
Output
For each test case, output a integer donating the number of one-and-half palindromic substrings.
Sample Input
1
ababcbabccbaabc
Sample Output
2
Hint
In the example input, there are two substrings which are one-and-half palindromic strings, a b a b abab abab and a b c b a b c abcbabc abcbabc.
他这个好像是奇数的,不知道能不能用主席树做,感觉主席树好想一点,可以直接回到过去的状态,但是禁止主席树只能用树状数组了,这个数据结构我用的比较少,所以一直在想怎么才能回到过去的状态或者可以再这个区间内直接求有几个可以匹配,最后才知道是如果j的回文半径可以到i的话,在for到i的时候直接将所有可以达到i的pos都放进去,之后再与i~j-1的半径进行比较,如果可以的话就加上去
注意ans会超int
#include<bits/stdc++.h>
using namespace std;
#define maxn 1000050
#define ll long long
char s[maxn];
char ss[2*maxn];
int p[2*maxn];
void manacher(char s[],int len)
{int i,j,len1;for(i=0;i<2*len+2;i++)ss[i]='#';for(i=0;i<len;i++)ss[i*2+2]=s[i];len1=len*2+1;ss[0]='$';int mx=0,id=0;int ans=0;for(int i=0;i<len1;i++){p[i]=mx>i?min(p[2*id-i],mx-i ):1;while(ss[i+p[i] ]==ss[i-p[i] ] )p[i]++;if(ans<p[i])ans=p[i];if(i+p[i]>mx){mx=i+p[i];id=i;}}
}
int sum[maxn];
int mlen;
int lowbit(int x)
{return x&(-x);
}
void add(int pos)
{for(int i=pos;i<=mlen;i+=lowbit(i))sum[i]++;
}
ll query(int pos)
{ll ans=0;for(int i=pos;i>=1;i-=lowbit(i))ans=(ll)ans+sum[i];return ans;
}
vector<int>vec[500005];
int len[500005];
int main()
{int t;scanf("%d",&t);while(t--){memset(sum,0,sizeof(sum));for(int i=0;i<500005;i++)vec[i].clear();scanf("%s",s);manacher(s,strlen(s));mlen=strlen(ss);ll ans=0;for(int i=2;i<mlen;i+=2){len[i/2]=(p[i]-2)/2;vec[i/2-len[i/2]].push_back(i/2);}mlen=strlen(s);for(int i=1;i<=mlen;i++){for(int j=0;j<vec[i].size();j++){add(vec[i][j]);}ans+=query(min(len[i]+i,mlen))-query(i);}printf("%lld\n",ans);}return 0;
}
这篇关于HDU6230 Palindrome —— 马拉车+树状数组的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!