比赛 20201031 评测结果 AAAAAAAA
题目名称 字串变换 最终得分 100
用户昵称 ShallowDream雨梨 运行时间 0.000 s
代码语言 C++ 内存使用 0.00 MiB
提交时间 2020-10-31 16:39:40
显示代码纯文本
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#include<string>
#include<queue>
using namespace std;
struct node
{
    string s;int num;
};
queue<node>q1,q2;
map<string,int>m1,m2;
string changings[6][2];
int cnt,ans;
string become(string now,int pos,int n,string tmp)
{
    return now.replace(pos,n,tmp);
}
bool dbfs()
{
    while(!q1.empty()&&!q2.empty())
    {
        node s1=q1.front(),s2;q1.pop();
        int len=s1.s.size();
        for(int i=0;i<len;i++)
            for(int j=0;j<cnt;j++)
            {
                int siz=changings[j][0].size();
                if(i+siz-1<len&&!s1.s.compare(i,siz,changings[j][0])&&!m1.count(become(s1.s,i,siz,changings[j][1])))
                {
                    s2.s=become(s1.s,i,siz,changings[j][1]);
                    s2.num=s1.num+1;
                    if(s2.num>10)return 0;
                    m1[s2.s]=s2.num;q1.push(s2);
                    if(m2.count(s2.s))
                    {
                        ans=s2.num+m2[s2.s];
                        return 1;
                    }
                }
            }
        s1=q2.front(),s2;q2.pop();
        len=s1.s.size();
        for(int i=0;i<len;i++)
            for(int j=0;j<cnt;j++)
            {
                int siz=changings[j][1].size();
                if(i+siz-1<len&&!s1.s.compare(i,siz,changings[j][1])&&!m2.count(become(s1.s,i,siz,changings[j][0])))
                {
                    s2.s=become(s1.s,i,siz,changings[j][0]);
                    s2.num=s1.num+1;
                    if(s2.num>10)return 0;
                    m2[s2.s]=s2.num;q2.push(s2);
                    if(m1.count(s2.s))
                    {
                        ans=s2.num+m1[s2.s];
                        return 1;
                    }
                }
            }
    }
    return 0;
}
int haha()
{
	freopen("string.in","r",stdin);
  freopen("string.out","w",stdout);
    
    node a,b;cin>>a.s>>b.s;
    if(a.s==b.s)
    {
        cout<<0;
        return 0;
    }
    a.num=b.num=0;
    q1.push(a);q2.push(b);
    while(cin>>changings[cnt][0]>>changings[cnt][1])cnt++;
    m1[a.s]=0;m2[b.s]=0;
    if(dbfs())cout<<ans;
    else puts("NO ANSWER!");
}
int sb=haha();
int main(){
  }