D. Two Strings Swaps(思维+讨论)

https://codeforces.com/problemset/problem/1006/D


思路:每个点的状态其实就4个点互相换的相关。

如果map四个点 +=2

map三个点讨论一下,一个坑点就是下方b串两个字符相同的时候也改一次就好

if(a[i]==b[i]||a[i]==b[j]||a[j]==b[i]||a[j]==b[j]||b[i]==b[j]) ans++;
           else ans+=2;

两个点的就两个情况

a a       a a

a c       b  b  讨论一下就好 

map1一个点的时候直接过

#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=1e5+1000;
typedef long long LL;
inline LL read(){LL x=0,f=1;char ch=getchar();	while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;}
char a[maxn],b[maxn];
map<char,LL>map1;
int main(void)
{
  cin.tie(0);std::ios::sync_with_stdio(false);
  LL n;cin>>n;
  for(LL i=1;i<=n;i++){
    cin>>a[i];
  }
  for(LL i=1;i<=n;i++){
    cin>>b[i];
  }
  LL ans=0;
  for(LL i=1,j=n;i<=n/2;i++,j--){
        map1.clear();
        map1[a[i]]++;map1[a[j]]++;
        map1[b[i]]++;map1[b[j]]++;
        if(map1.size()==1) continue;
        else if(map1.size()==4) ans+=2;
        else if(map1.size()==3){
           if(a[i]==b[i]||a[i]==b[j]||a[j]==b[i]||a[j]==b[j]||b[i]==b[j]) ans++;
           else ans+=2;
        }
        else{
            bool flag=1;
            if(map1.size()==2){
                for(auto j:map1){
                    if(j.second!=2){
                        flag=0;
                        break;
                    }
                }
            }
            if(flag==1) continue;
            if(flag==0){
                ans++;
            }
        }
  }
  if(n&1){
     if(a[n/2+1]!=b[n/2+1]) ans++;
  }
  cout<<ans<<"\n";
return 0;
}

猜你喜欢

转载自blog.csdn.net/zstuyyyyccccbbbb/article/details/114934320
今日推荐