WA60pts,玄关求调
查看原帖
WA60pts,玄关求调
1652696
Summer_river楼主2025/7/28 15:51
#include<bits/stdc++.h>
#define int long long
#define ull unsigned long long
#define N 1010
using namespace std;
inline long long read()
{
	long  long x=0,f=1;char ch=getchar();
	while (ch<'0'||ch>'9'){if (ch=='-')f=-1;ch=getchar();}
	while (ch>='0'&&ch<='9'){x=x*10+ch-48;ch=getchar();}
	return x*f;
}
queue<string>q;
map<string,int>mp,vis;
signed main(){
	string s;
	cin>>s;
	q.push(s);
	while(!q.empty())
	{
		string nw=q.front();q.pop();
		int len=nw.size();
		for(int i=0;i<len;i++) 
		{
			for(int j=i+1;j<len;j++)
			{
				string x=nw;
				swap(x[i],x[j]);
				if(!vis[x])
                {
                    mp[x]=mp[nw]+1;
                    q.push(x);
                }
                vis[x]=1;
			}
		}
		for(int i=0;i<len;i++)
		{
			string x=nw;
			x.erase(i,1); 
			if(!vis[x]&&x.size()>=1)
            {
                mp[x]=mp[nw]+1;
                q.push(x);
            }
            vis[x]=1;
		}
        if(len==s.size()) continue;
		for(int i=1;i<len;i++)
		{
			int l=nw[i-1]-'0',r=nw[i]-'0';
			if(l>=r) continue;
			for(int j=nw[i-1]-'0'+1;j<nw[i]-'0';j++)
			{
				string x=nw;
				x.insert(i,to_string(j));
				if(!vis[x])
                {
                   mp[x]=mp[nw]+1;
                    q.push(x);
                }
                vis[x]=1;
			}
		}
	}
	int m;
    m=read();
	for(int i=1;i<=m;i++)
	{
		string c;
		cin>>c;
        if(c==s){cout<<"0"<<endl;continue;}
		if(mp[c]==0) printf("-1\n");
		else printf("%lld\n",mp[c]);
	}
	return 0;
}


2025/7/28 15:51
加载中...