【ZOJ 4060】Flippy Sequence
阿新 • • 發佈:2018-11-07
zoj pri lld n) pac %s col std bubuko
【鏈接】 我是鏈接,點我呀:)
【題意】
【題解】
按照兩個區間的排列方式
我們可以分成以下幾種情況
會發現這兩個區間的作用
最多只能把兩段連續不同的區間變為相同。
那麽寫個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\n",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\n",ans); continue; } if (cnt==2){ ans+=6; printf("%lld\n",ans); continue; } puts("0"); } return 0; }
【ZOJ 4060】Flippy Sequence