【hihocoder1251】Today is a rainy day

#include<bits/stdc++.h>
const int N=120;
const int M=5e4+5;
const int inf=0x3f3f3f3f;
using namespace std;
char s1[N],s2[N];
int c[6],f[6][6],dis[M];
inline int Index(int *a){
    int ans=0;
    for(int i=0;i<6;i++)ans=ans*6+a[i];
    return ans;
}
inline void hs(int x,int *a){
    for(int i=5;i>=0;i--)a[i]=x%6,x/=6;
}
inline void bfs(){
    memset(dis,127,sizeof(dis));int b[6];
    for(int i=0;i<6;i++)b[i]=i;
    int x=Index(b);dis[x]=0;
    static queue<int> q;q.push(x);
    while(!q.empty()){
        int u=q.front();q.pop();
        hs(u,b);
        for(int i=0;i<6;i++)for(int j=0;j<6;j++){
            int t[6];memcpy(t,b,sizeof(t));
            for(int k=0;k<6;k++)if(t[k]==i)t[k]=j;
            int v=Index(t);
            if(dis[v]>dis[u]+1)dis[v]=dis[u]+1,q.push(v);
        }
    }
}
int main(){
    freopen("ff.in","r",stdin);
    freopen("dick.out","w",stdout);
    bfs();
    while(~scanf("%s%s",s1,s2)){
        int len=strlen(s1);
        memset(c,0,sizeof(c));memset(f,0,sizeof(f));
        for(int i=0;i<len;i++){
            int x=s2[i]-'1',y=s1[i]-'1';c[x]++;f[x][y]++;
        }
        int ans=inf,b[6];
        for(int i=0;i<M;i++){
            hs(i,b);int tmp=dis[i];
            for(int j=0;j<6;j++)tmp+=c[j]-f[j][b[j]];
            ans=min(ans,tmp);
        }
        printf("%d\n",ans);
    }
}

 

posted @ 2017-09-25 11:00 zcysky 阅读(...) 评论(...) 编辑 收藏