1. 程式人生 > >noip提高組2002 字串變換(luogu P1032 )

noip提高組2002 字串變換(luogu P1032 )

log pla logs struct mil == main include ron

原題鏈接:https://www.luogu.org/problem/show?pid=1032

對於此題,我只想說,string雖然慢,但是功能太強。

還有,map大法好,不但跑的飛快,而且簡化了算法,普通的bfs就解決了。

#include<cstdio>
#include<map>
#include<queue>
#include<cstring>
#include<iostream>
using namespace std;
struct node
{
    string s;
    int l;
}st;
queue<node>q;
map
<string,int>vis; string a,b,c[25][2]; int n=1; int main() { cin>>a>>b; while(cin>>c[n][0]>>c[n][1]) n++; n--; st.s=a;st.l=0; q.push(st); while(!q.empty()) { node t=q.front();q.pop(); if(t.s==b&&t.l<11) { printf(
"%d",t.l); return 0; } if(vis[t.s]==0) { vis[t.s]=1; for(int i=1;i<=n;i++) { if(t.s.find(c[i][0])>=0) { for(int j=t.s.find(c[i][0]);j>=0&&j<=t.s.size()-c[i][0].size();j=t.s.find(c[i][0
],j+1)) { node tmp; tmp.s=t.s;tmp.l=t.l+1; tmp.s.replace(j,c[i][0].size(),c[i][1]); q.push(tmp); } } } } } printf("NO ANSWER!"); return 0; }

noip提高組2002 字串變換(luogu P1032 )