codevs1099字串變換(Bfs)

來源:互聯網
上載者:User

標籤:

/*最少步數問題 妥妥的Bfs很顯然隊列裡存的是串(可能存個數也可以 就像8數位那樣)然後每次隊首元素弄出來 能換的都換一遍最後每次換完的新串入隊前先判斷到頭了沒最後說一句 String大法好0.0 */#include<iostream>#include<cstdio>#include<cstring>#include<cstdlib>using namespace std;int ans,n=1,head,tail,t[2000050];string s1,s2,sl[10],sr[10],q[2000050];int main(){    cin>>s1>>s2;    int i,j,k;    while(cin>>sl[n]>>sr[n])n++;    n--;//處理變換規則      q[++tail]=s1;    while(t[++head]<10&&head<=tail)      {          for(i=1;i<=n;i++)//每一種規則都嘗試 反正資料小 任性            for(j=0;j<=q[head].length()-1;j++)//可能有好幾個地方可以換 所以枚舉一下              if(q[head].compare(j,sl[i].length(),sl[i])==0)//如果包含給出的串                {               q[++tail]=q[head];                 t[tail]=t[head]+1;                 q[tail].replace(j,sl[i].length(),sr[i]);//將找到的字串入隊 然後換掉                  if(q[tail]==s2)                   {                     cout<<t[tail];                     return 0;                 }             }      }          cout<<"NO ANSWER!";    return 0;}

 

codevs1099字串變換(Bfs)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.