2018 acm_icpc 青岛 C题


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:

  • Let , where , be a valid operation pair denoting that DreamGrid selects integers  and  for the first operation and integers and  for the second operation;
  • Let , where , be another valid operation pair denoting that DreamGrid selects integers  and  for the first operation and integers  and  for the second operation.
  •  and  are considered different, if there exists an integer  () such that .

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).


http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5817

#include <bits/stdc++.h>
using namespace std;
char a[1000010],b[1000010];
int main(){
	int n,t;
	scanf("%d",&t);
	while(t--){
		scanf("%d",&n);
		scanf("%s %s",a,b);
		int sum=0;//不同的区间数 
		int w1=0;
		int ww=0;//有两个区间不同时,记录中间相同的个数 
		int x1=0,y1=0; 
		for(int i=0;i<n;i++){
			if(a[i]==b[i]){
				if(sum==1){
					x1++;
					ww++;
				} 
				else{
					y1++;
				}
			}
			else{
				if(a[i-1]==b[i-1] || i==0){//前面相同同或第一个不同,区间加一 
					sum++;
				}
				if(sum==3){
					break;
				} 
				if(sum==1)//记录第一个不同区间的个数,为输出sum等于1时所用 
					w1++;
			}
		}
		if(sum==0){
			printf("%d\n",(n+1)*n/2);
		} 
		else if(sum==3){//三个或以上 
			printf("0\n");
		}
		else if(sum==1){//只有一个区间不同
			if(w1==n){
				printf("%d\n",(w1-1)*2);
			} 
			else
				printf("%d\n",(w1-1)*2+(x1+y1)*2);
		}
		else{
			printf("6\n");
		}
	}
} 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值