这篇题解献给那些乱搞的人,请不会此题的暴搜解法的萌新不要被误导
打眼一看,此题:
1.多串匹配
2.bfs
所以——我们可以跑双向bfs,用AC自动机来干多串匹配(因为此题数据太水,而常数过大,被暴搜吊着打)
具体没什么细节,就是思路骚+码量大
祭出并不优美的代码
//这题除了小写字母外还有数字和大写字母,这一点要注意
#include<bits/stdc++.h>
using namespace std;
#define rg register
#define int long long
#define inf (1ll<<60)
#define gc() getchar()
#define pc(a) putchar(a)
char buffer[55];
inline int read()
{
int x=0,f=1;
char c=gc();
while(c<'0'||c>'9')
{
if(c=='#') return -1;
if(c=='-') f=-f;
c=gc();
}
while(c>='0'&&c<='9') x=x*10+c-'0',c=gc();
return x*f;
}
inline void write(int x,char end='\n')
{
if(x==0)
{
pc('0');
pc(end);
return;
}
int en=0;
if(x<0ll) pc('-'),x=-x;
while(x)
{
buffer[++en]=x%10+'0';
x/=10;
}
for(int i=en;i;i--) pc(buffer[i]);
pc(end);
return;
}
#define ma 105//105就够了
//哈希
const int h=73,mod=19260817;
inline int id(char c)
{
if(c>='0'&&c<='9') return c-'0'+1;
if(c>='a'&&c<='z') return c-'a'+1+10;
if(c>='A'&&c<='Z') return c-'A'+1+36;
}
inline unsigned int has(char s[])
{
int n=strlen(s+1);
unsigned int ans=0;
for(int i=1;i<=n;i++)
ans=(ans*h+id(s[i]));
return ans;
}
//AC自动机板子
int son[ma][75],en[ma],tot,fail[ma];
#define root 0
inline void New(int &i){i=++tot;}
inline void ins(char s[],int iid)
{
int n=strlen(s+1);
int now=root;
for(int i=1;i<=n;i++)
{
int nxt=son[now][id(s[i])];
if(!nxt)
New(nxt),son[now][id(s[i])]=nxt;
now=nxt;
}
en[now]|=1ll<<(iid-1);
return;
}
inline void getfail()
{
queue<int> q;
for(int i=1;i<=73;i++)
if(son[root][i])
q.push(son[root][i]);
while(!q.empty())
{
int x=q.front();
q.pop();
for(int i=1;i<=73;i++)
{
int t=son[x][i];
if(t)
{
fail[t]=son[fail[x]][i];
en[t]|=en[fail[t]];
q.push(t);
}
else
son[x][i]=son[fail[x]][i];
}
}
return;
}
//变换及涉及字符的长度
char x[10][2005],y[10][2005],len[2][10];
//双向bfs
struct pq
{
char s[2005];
int dep;
pq(){memset(s,0,sizeof(s));}
inline char operator [] (int i){return s[i];}
};
bool operator < (pq x,pq y)
{
return x.dep<y.dep;
}
bool operator > (pq x,pq y)
{
return y>x;
}
inline pq replace(pq x,char s[],int l,int r)//替换
{
pq y;
int len=strlen(s+1);
int siz=strlen(x.s+1);
int now=l;
for(int i=1;i<l;i++) y.s[i]=x.s[i];
for(int i=1;i<=len;i++) y.s[now]=s[i],now++;
for(int i=r+1;i<=siz;i++) y.s[now]=x.s[i],now++;
return y;
}
unordered_map<unsigned int,int> vis1,vis2;
queue<pq> q;
inline void bfspre(char s[],int dep_max)//反向搜
{
while(!q.empty()) q.pop();
pq xx;
memcpy(xx.s,s,sizeof(xx.s));
xx.dep=1;
vis1[has(xx.s)]=1;
q.push(xx);
while(!q.empty())
{
pq topq=q.front();
q.pop();
if(topq.dep>dep_max) continue;
int now=root;
int siz=strlen(topq.s+1);
for(int i=1;i<=siz;i++)
{
now=son[now][id(topq.s[i])];
for(int j=1;j<=6;j++)
if(en[now]&(1ll<<(j-1)))
{
pq nxt=replace(topq,x[j],i-len[1][j]+1,i);
nxt.dep=topq.dep+1;
if(vis1[has(nxt.s)])
{
continue;
}
vis1[has(nxt.s)]=topq.dep+1;
q.push(nxt);
}
}
}
return;
}
inline int bfs(char s[],int dep_max)//正向搜
{
while(!q.empty()) q.pop();
pq xx;
memcpy(xx.s,s,sizeof(xx.s));
xx.dep=0;
if(vis1[has(xx.s)]) return vis1[has(xx.s)]-1;
vis2[has(xx.s)]=1;
q.push(xx);
while(!q.empty())
{
pq topq=q.front();
q.pop();
if(topq.dep>=dep_max) continue;
int now=root;
int siz=strlen(topq.s+1);
for(int i=1;i<=siz;i++)
{
now=son[now][id(topq.s[i])];
for(int j=1;j<=6;j++)
if(en[now]&(1ll<<(j-1)))
{
pq nxt=replace(topq,y[j],i-len[0][j]+1,i);
nxt.dep=topq.dep+1;
if(vis1[has(nxt.s)]) return topq.dep+vis1[has(nxt.s)];
if(vis2[has(nxt.s)]) continue;
vis2[has(nxt.s)]=1;
q.push(nxt);
}
}
}
return -1;//无解返回-1
}
char s1[2005],s2[2005];
signed main()
{
scanf("%s%s",s1+1,s2+1);
int n=1;
while(~scanf("%s%s",x[n]+1,y[n]+1)) n++;
n--;
for(int i=1;i<=n;i++)
len[0][i]=strlen(x[i]+1),len[1][i]=strlen(y[i]+1);
for(int i=1;i<=n;i++) ins(y[i],i);//先对y建ACAM,因为要反向搜
getfail();
bfspre(s2,3);//研究发现(划),37分最快
tot=0;
memset(son,0,sizeof(son));
memset(en,0,sizeof(en));
memset(fail,0,sizeof(fail));
for(int i=1;i<=n;i++)//再对x建
ins(x[i],i);
getfail();
int ans=bfs(s1,7);
if(~ans) write(ans);
else puts("NO ANSWER!");//无解
// write(bfs(s1,7));
}