C 分类讨论+思路
#include<bits/stdc++.h> using namespace std; typedef long long ll; const int N=1e6+10; string s,t; int a[N]; vector<int> num; #define mp make_pair #define p pair<int,int> vector<p> st; int main(){ ios::sync_with_stdio(false); int T; cin>>T; while(T--){ st.clear(); ll n; cin>>n; cin>>s>>t; s=" "+s; t=" "+t; for(int i=1;i<=n;i++)if(s[i]!=t[i])a[i]=1;else a[i]=0; a[n+1]=0; int l=-1,r=-1; for(int i=1;i<=n+1;i++){ if(a[i]==1&&a[i-1]!=1){ l=i,r=i; }else if(a[i]==1&&a[i-1]==1)r=i; else if(a[i]==0){ if(l!=-1&&r!=-1) st.push_back(mp(l,r)); l=-1,r=-1; } } if(st.size()==0){ cout<<n*(n+1)/2<<endl; }else if(st.size()==1){ ll l=st[0].first-1; ll r=n-st[0].second; ll op=st[0].second-st[0].first+1; cout<<l*2+r*2+(op-1ll)*2<<endl; }else if(st.size()==2){ cout<<6<<endl; }else{ cout<<0<<endl; } } return 0; }
D 枚举首位,后面都是固定的
#include<bits/stdc++.h> using namespace std; typedef long long ll; const int N=2e5+10; char C[N]; int a[N],b[N],c[N]; int main() { //ios::sync_with_stdio(false); int T; scanf("%d",&T); while(T--) { int n,m; scanf("%d%d%s",&n,&m,C); for(int i=0; C[i]!='