Flippy Sequence(青島icpc2018)
Flippy Sequence Time Limit: 1 Second Memory Limit: 65536 KB DreamGrid has just found two binary sequences and ( for all ) from his virtual machine! He would like to perform the operation described below exactly twice, so that holds for all after the two operations. The operation is: Select two integers and (), change to for all . DreamGrid would like to know the number of ways to do so. We use the following rules to determine whether two ways are different:
Input There are multiple test cases. The first line of the input contains an integer , indicating the number of test cases. For each test case: The first line contains an integer (), indicating the length of two binary sequences. The second line contains a string () of length , indicating the first binary sequence. The third line contains a string () of length , indicating the second binary sequence. It's guaranteed that the sum of in all test cases will not exceed . Output For each test case, output an integer denoting the answer. Sample Input 3 1 1 0 2 00 11 5 01010 00111 Sample Output 0 2 6 Hint For the second sample test case, there are two valid operation pairs: (1, 1, 2, 2) and (2, 2, 1, 1). For the third sample test case, there are six valid operation pairs: (2, 3, 5, 5), (5, 5, 2, 3), (2, 5, 4, 4), (4, 4, 2, 5), (2, 4, 4, 5) and (4, 5, 2, 4). Author: LIN, Xi Source: The 2018 ACM-ICPC Asia Qingdao Regional Contest |
Copyright @ 2001-2018, Zhejiang University ACM/ICPC Team, All rights reserved.
Flippy Sequence Time Limit: 1 Second Memory Limit: 65536 KB DreamGrid has just found two binary sequences and ( for all ) from his virtual machine! He would like to perform the operation described below exactly twice, so that holds for all after the two operations. The operation is: Select two integers and (), change to for all . DreamGrid would like to know the number of ways to do so. We use the following rules to determine whether two ways are different:
Input There are multiple test cases. The first line of the input contains an integer , indicating the number of test cases. For each test case: The first line contains an integer (), indicating the length of two binary sequences. The second line contains a string () of length , indicating the first binary sequence. The third line contains a string () of length , indicating the second binary sequence. It's guaranteed that the sum of in all test cases will not exceed . Output For each test case, output an integer denoting the answer. Sample Input 3 1 1 0 2 00 11 5 01010 00111 Sample Output 0 2 6 Hint For the second sample test case, there are two valid operation pairs: (1, 1, 2, 2) and (2, 2, 1, 1). For the third sample test case, there are six valid operation pairs: (2, 3, 5, 5), (5, 5, 2, 3), (2, 5, 4, 4), (4, 4, 2, 5), (2, 4, 4, 5) and (4, 5, 2, 4). Author: LIN, Xi Source: The 2018 ACM-ICPC Asia Qingdao Regional Contest |
Copyright @ 2001-2018, Zhejiang University ACM/ICPC Team, All rights reserved.
#include <iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
const int maxn=1010001;
char a[maxn];
char b[maxn];
int main()
{
int t;
long long n;
scanf("%d",&t);
while(t--)
{
scanf("%lld",&n);
scanf("%s%s",a,b);
int x,y;
x=y=0;
for(int i=0; a[i]; i++)
{
if(a[i]==b[i])
{
if(y!=0)
{
x++;
y=0;
}
}
else
y++;
}
if(y!=0)x++;
if(x==2)
printf("6\n");
else if(x==0)
printf("%lld\n",n*(n+1)/2);
else if(x==1)
printf("%lld\n",(n-1)*2);
else printf("0\n");
}
return 0;
}