SPOJ-STC02 - Antisymmetry(双hash,回文典型做法)

题目链接

STC02 - Antisymmetry

no tags 

Byteasar studies certain strings of zeroes and ones. Let S be such a string. By Sr we will denote the reversed (i.e., "read backwards") string S, and by SI we will denote the string obtained from S by changing all the zeroes to ones and ones to zeroes.

Byteasar is interested in antisymmetry, while all things symmetric bore him. Antisymmetry however is not a mere lack of symmetry. We will say that a (nonempty) string S is antisymmetric if, for every position i in S, the i-th last character is different than the i-th (first) character. In particular, a string S consisting of zeroes and ones is antisymmetric if and only if S=SIr. For example, the strings 00001111 and 010101 are antisymmetric, while 1001 is not.

In a given string consisting of zeroes and ones we would like to determine the number of contiguous nonempty antisymmetric fragments. Different fragments corresponding to the same substrings should be counted multiple times.

Input

The first line of the standard input contains an integer N (1 <= N <= 500000) that denotes the length of the string. The second line gives a string of 0 and/or 1 of length N. There are no spaces in the string.

Output

The first and only line of the standard output should contain a single integer, namely the number of contiguous (non empty) fragments of the given string that are antisymmetric.

 

Example

For the input data:

8
11001011

the correct result is:

7

Antisymmetric fragments are: 01 (occurs twice), 10 (also twice), 01011100, and 001011.





题意

定义字符串S为原串,SR为原串反转的到的字符串,SL为将原串中所有1变成0,0变成1得到的字符串。

一个非空字符串,对于他的每个第i个位置的字符和倒数第i个位置的字符都不同,那么这个串为antisymmetry。

特别的,如果一个字符串仅有0和1组成,那么当且仅当S=SLR的时候,他为antisymmetry。

对于一个给定的01串,我们需要确定这个01串中连续.非空.且满足antisymmetric的片段的数目。

不同的片段,对应相同的子串的情形也需要计数。

字符串长度 N (1 <= N <= 500000)。


题解

枚举片段的中间位置,看向两边能延长几个位置。显然这个长度可以二分,所以问题变成了一个子串按位取反以后是否和另一个子串翻过来相等。因而HASH一下就可以了。

注意下面的双hash法

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
#include<stack>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define fi first
#define se second
typedef vector<int> VI;
typedef unsigned long long ll;
typedef pair<int,int> PII;
const int inf=0x3fffffff;
const ll mod1=1000000007;   //双hash
const ll mod2=2147483647;
const int maxn=1000000+100;
ll A1[maxn],A2[maxn];
ll f1[maxn],f2[maxn],ff1[maxn],ff2[maxn];
char s[maxn];
int main()
{
    int n;
    scanf("%d",&n);
    scanf("%s",s+1);
    A1[0]=A2[0]=1;
    rep(i,1,maxn) A1[i]=A1[i-1]*2%mod1;
    rep(i,1,maxn) A2[i]=A2[i-1]*2%mod2;
    ll ans=0;
    rep(i,1,n+1)
    f1[i]=(f1[i-1]*2+s[i]-'0')%mod1,ff1[i]=(ff1[i-1]*2+s[i]-'0')%mod2;
    per(i,1,n+1)
    f2[i]=(f2[i+1]*2+(s[i]-'0')^1)%mod1,ff2[i]=(ff2[i+1]*2+(s[i]-'0')^1)%mod2;
    rep(i,1,n+1)
    {
        int l=0,r=min(i,n-i);
        int res=0;
        while(l<=r)
        {
            int mid=(l+r)/2;
            if((f1[i]-f1[i-mid]*A1[mid]%mod1+mod1)%mod1==(f2[i+1]-f2[i+mid+1]*A1[mid]%mod1+mod1)%mod1
               &&(ff1[i]-ff1[i-mid]*A2[mid]%mod2+mod2)%mod2==(ff2[i+1]-ff2[i+mid+1]*A2[mid]%mod2+mod2)%mod2)
                l=mid+1,res=mid;
            else r=mid-1;
        }
        ans+=(ll)res;
    }
    printf("%lld\n",ans);
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值