1. 程式人生 > >字串變換

字串變換

inline 認識 大數 return count ble new queue ostream

字串變換

一個簡單的 \(bfs\) ,只是擴展結點時比較"另類",還有一點叠代加深搜索的味道
數據範圍較小,無需\(kmp\) ,暴力判斷就好

認識到了兩點

  1. \(\because\) 無符號數相減不會出現負數,\(\therefore\) 當小數減大數時會得到一個很大的數. \(\therefore\) 應該避免無符號數相減
  2. 在提交程序之前一定要仔細檢查是否有為了調試而輸出的中間結果!
#include <iostream>
#include <cstdio>
#include <string>
#include <queue>
#include <map>
using namespace std;
const int MAXN=30;
string a[MAXN],b[MAXN];
map<string,int>vis;
struct state
{
    string s;
    int dist;
    state(string ss,int d):s(ss),dist(d){}
};

queue<state>Q;

int main()
{
    string A,B,c,d;
    int ans = 100+1;
    cin >> A >> B;
    
    int n=0;
    while(cin >> c >> d)
    {
        a[++n] = c;
        b[n] = d;
    }

    Q.push(state(A,0));
    
    while(!Q.empty())
    {
        state u = Q.front();Q.pop();
        
        string& s=u.s;
        
        if(s == B)
        {
            ans = u.dist;
            break;
        }
        
        for(int i = 1; i <= n; i++)
        {
            const int len1 = s.length(), len2 = a[i].length() ;
            //不能用 s.length() - a[i].length(),因為無符號數相減,
            //若被減數小於減數,結果不是負數而是一個很大的正數,會導致 j 超出範圍!
            for(int j = 0;j <= len1 - len2; j++)
            {
                if(s.substr(j, a[i].length() ) == a[i])
                {
                    string v = s;
                    v.replace(j,a[i].length(),b[i]);
                    if(!vis.count(v))
                    {
                        vis[v]=1;
                        Q.push(state(v, u.dist + 1));
                    }
                }
            }
        }
    }

    if(ans <= 10)cout << ans;
    else cout << "NO ANSWER!";
    return 0;
}

字串變換