題目連接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4060ios
題意:c++
給出兩個 $0,1$ 字符串 $S,T$,如今你有兩次對 $S$ 做區間翻轉($0 \rightarrow 1,1 \rightarrow 0$)的操做,spa
用四元組 $(l_1,r_1,l_2,r_2)$ 表示,表明第一次翻轉區間 $[l_1,r_1]$,第二次翻轉區間 $[l_2,r_2]$。3d
問你有多少個四元組能夠使得 $S=T$。code
題解:blog
把 $S$ 儘量少地分割成若干個子串。若某一子串和相應區間的 $T$ 同樣,記做 $B$;反之,則記做 $A$。ci
所以若 $A$ 的數量大於兩個,就不可能經過區間翻轉兩次使得 $S=T$,所以 $A$ 最可能是兩個。字符串
分類討論:get
AC代碼:string
#include<bits/stdc++.h> using namespace std; int n; string s,t; int main() { ios::sync_with_stdio(0); cin.tie(0); int T; cin>>T; while(T--) { cin>>n>>s>>t; int cnt=0; for(int i=0;i<n;i++) { if((i==0 || s[i-1]==t[i-1]) && s[i]!=t[i]) cnt++; } if(cnt>2) cout<<"0\n"; else if(cnt==2) cout<<"6\n"; else if(cnt==1) cout<<(2*n-2)<<'\n'; else cout<<((long long)n*(n+1)/2)<<'\n'; } }