【ZOJ 4060】Flippy Sequence

【链接】 我是链接,点我呀:)
【题意】

【题解】

按照两个区间的排列方式 我们可以分成以下几种情况 ![](https://img2018.cnblogs.com/blog/1251265/201811/1251265-20181107092457518-1766840129.png) 会发现这两个区间的作用 最多只能把两段连续不同的区间变为相同。 那么写个for处理出连续不相同的一段的个数cnt。 根据上面的排列方式。 算出每个cnt对应的答案即可。 别忘了有些情况可以乘2.

【代码】

#include <bits/stdc++.h>
#define ll long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
using namespace std;

const int N = 1e6;

int n;
char s[N+10],t[N+10];
vector<pair<int,int> > v;

int main(){
	#ifdef ccy
			freopen("rush.txt","r",stdin);
	#endif
	int T;
	scanf("%d",&T);
	while (T--){
		scanf("%d",&n);
		scanf("%s",s+1);
		scanf("%s",t+1);
		v.clear();
		int l = 0,r = 0;
		rep1(i,1,n){
		 	if (s[i]!=t[i]){
		 	 	if (l==0) l = i;
		 	 	r = i;
		 	}else{
		 	 	if (l!=0){
		 	 	 	v.push_back(make_pair(l,r));
		 	 	 	l = 0;
		 	 	}
		 	}
		}
		if (l!=0) v.push_back(make_pair(l,r));
		int cnt = v.size();
		ll ans = 0;
		if (cnt==0){
			ans+=1LL*n*(n+1)/2;
			printf("%lld
",ans);
			continue;		 	
		}
		if (cnt==1){
		 	ans+=1LL*(v[0].first-1+n-v[0].second)*2;
		 	ans+=1LL*(v[0].second-v[0].first)*2;
		    printf("%lld
",ans);
		 	continue;
		}
		if (cnt==2){
		    ans+=6;
		    printf("%lld
",ans);
			continue;
		}	
		puts("0");
	}
 	return 0;
}
原文地址:https://www.cnblogs.com/AWCXV/p/9920645.html