codeforces 1186C----Vus the Cossack and Strings

题意:

Vus the Cossack has two binary strings, that is, strings that consist only of "0" and "1". We call these strings aa and bb. It is known that |b|≤|a||b|≤|a|, that is, the length of bb is at most the length of aa.

The Cossack considers every substring of length |b||b| in string aa. Let's call this substring cc. He matches the corresponding characters in bb and cc, after which he counts the number of positions where the two strings are different. We call this function f(b,c)f(b,c).

For example, let b=00110b=00110, and c=11000c=11000. In these strings, the first, second, third and fourth positions are different.

Vus the Cossack counts the number of such substrings cc such that f(b,c)f(b,c) is even.

For example, let a=01100010a=01100010 and b=00110b=00110. aa has four substrings of the length |b||b|: 0110001100, 1100011000, 1000110001, 0001000010.

  • f(00110,01100)=2f(00110,01100)=2;
  • f(00110,11000)=4f(00110,11000)=4;
  • f(00110,10001)=4f(00110,10001)=4;
  • f(00110,00010)=1f(00110,00010)=1.

Since in three substrings, f(b,c)f(b,c) is even, the answer is 33.

Vus can not find the answer for big strings. That is why he is asking you to help him.

 Input

The first line contains a binary string aa (1≤|a|≤1061≤|a|≤106) — the first string.

The second line contains a binary string bb (1≤|b|≤|a|1≤|b|≤|a|) — the second string.

 Output

Print one number — the answer.

算法分析:

主要是找到一个规律,当子串和匹配串的1的个数的奇偶性相同时,则f函数的结果就是偶数

AC代码:

import java.math.BigInteger;
import java.util.*;
public class test{
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        String str=scan.nextLine();
        String str1=scan.nextLine();
        int len=str.length();
        int len1=str1.length();
        int flag=0,num=0,num1=0;
        for(int i=0;i<len1;i++){
            if(str.charAt(i)=='1')num++;
            if(str1.charAt(i)=='1')num1++;
        }
        if(num%2==num1%2)flag++;
        for(int i=len1,j=0;i<len;i++,j++){
            if(str.charAt(i)=='1')num++;
            if(str.charAt(j)=='1')num--;
            if(num%2==num1%2)flag++;
        }
        System.out.println(flag);
    }
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值