ZOJ Problem Set
阿新 • • 發佈:2018-12-23
當有0個連續區間的時候,答案為n*(n+1)/2
當有1個連續區間的時候,答案為(n-1)*2
當有兩個連續區間的時候,答案為6
當有三個及以上的時候,答案為0
程式碼如下:
#include <cstdio> #include <cstring> #include <algorithm> #include <iostream> using namespace std; const int maxn=1e6+5; typedef long long ll; ll T,n; char s[maxn],t[maxn]; ll x,y; int main() { scanf("%lld",&T); while (T--) { ll num=0; scanf("%lld%s%s",&n,s,t); for (int i=0;i<n;i++) { if(s[i]!=t[i]) { x=i; while(s[i]!=t[i]&&i<n) { i++; } y=i-1; num++; } } if(num==0) { printf("%lld\n",n*(n+1)/2); } else if(num==1) { printf("%lld\n",2*(n-1)); } else if(num==2) { printf("6\n"); } else { printf("0\n"); } } return 0; }