# BZOJ2084: [Poi2010]Antisymmetry

## 2084: [Poi2010]Antisymmetry

Time Limit: 10 Sec  Memory Limit: 259 MB
Submit: 187  Solved: 125
[Submit][Status]

8
11001011

## Sample Output

7

hint
7个反对称子串分别是：01(出现两次), 10(出现两次), 0101, 1100和001011

## Source

nlogn 貌似可过？

1     scanf("%s",s+1);
2     for1(i,n)st[i<<1]=(s[i]=='0'?0:2);
3     n<<=1;n++;
4     for(int i=1;i<=n;i+=2)st[i]=1;
    mx=id=0;
for(int i=1;i<=n;i+=2)
{
if(mx>i)p[i]=min(p[2*id-i],mx-i);else p[i]=1;
for(;i-p[i]>0&&i+p[i]<=n&&st[i-p[i]]+st[i+p[i]]==2;p[i]++);
if(p[i]+i>mx)mx=p[i]+i,id=i;
}

st[i-p[i]]+st[i+p[i]]==2

manacher算法主要是一种思想：减少冗余的计算，充分利用之前已经计算过的信息。

 1 #include<cstdio>
2 #include<cstdlib>
3 #include<cmath>
4 #include<cstring>
5 #include<algorithm>
6 #include<iostream>
7 #include<vector>
8 #include<map>
9 #include<set>
10 #include<queue>
11 #include<string>
12 #define inf 1000000000
13 #define maxn 2000000+1000
14 #define maxm 500+100
15 #define eps 1e-10
16 #define ll long long
17 #define pa pair<int,int>
18 #define for0(i,n) for(int i=0;i<=(n);i++)
19 #define for1(i,n) for(int i=1;i<=(n);i++)
20 #define for2(i,x,y) for(int i=(x);i<=(y);i++)
21 #define for3(i,x,y) for(int i=(x);i>=(y);i--)
22 #define mod 1000000007
23 using namespace std;
25 {
26     int x=0,f=1;char ch=getchar();
27     while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
28     while(ch>='0'&&ch<='9'){x=10*x+ch-'0';ch=getchar();}
29     return x*f;
30 }
31 int n,st[maxn],p[maxn],mx,id;
32 char s[maxn];
33 int main()
34 {
35     freopen("input.txt","r",stdin);
36     freopen("output.txt","w",stdout);
38     scanf("%s",s+1);
39     for1(i,n)st[i<<1]=(s[i]=='0'?0:2);
40     n<<=1;n++;
41     for(int i=1;i<=n;i+=2)st[i]=1;
42     mx=id=0;
43     for(int i=1;i<=n;i+=2)
44     {
45         if(mx>i)p[i]=min(p[2*id-i],mx-i);else p[i]=1;
46         for(;i-p[i]>0&&i+p[i]<=n&&st[i-p[i]]+st[i+p[i]]==2;p[i]++);
47         if(p[i]+i>mx)mx=p[i]+i,id=i;
48     }
49     ll ans=0;
50     for(int i=1;i<=n;i+=2)ans+=(p[i]-1)>>1;
51     printf("%lld\n",ans);
52     return 0;
53 }
View Code

posted @ 2014-10-02 11:20  ZYF-ZYF  Views(189)  Comments(0Edit  收藏  举报