(BFS)

       對於這個題的思路主要是BFS,從頭開始分別對三個操作進行使用,直到最後需要操作的兩個字符串相等或都爲空,需要注意的是在兩個字符串長度不等但開頭的部分相等,例如A = casfg,B = ca,在這種情況下只能進行第一個操作,而對於A = ca,B = casfg,這時只能進行插入字符操作。具體代碼如下:

#include <map>  
#include <cmath>  
#include <queue>  
#include <cstdio>  
#include <string>  
#include <cstring>  
#include <iostream>  
#include <algorithm> 
#include <sstream> 
#include <time.h> 
#include <vector>
#include <list>
#include <stack>
using namespace std;
bool Flag = 0;
int result = 0,save = 0;
struct Judge {
	string A, B;
	int num = 0;
};

queue<Judge>store;					//先進先出隊列

//0代表刪除字符,1代表插入字符,2代表修改一個字符
//廣度優先搜索,路徑數組、搜索標誌數組、起始點、發現的點的數量
void BFS()
{
	while (!store.empty()) 
	{
		Judge front = store.front();
		if (front.A == front.B)
		{
			Flag = 1;
			result = front.num;
			//cout << "o:" << result << endl;
			return;
		}
		if (Flag == 1)
			return;
		int j = 0;
		while (j < front.A.length() && j < front.B.length() && front.A[j] == front.B[j])
			j++;
		
		for (int i = 0; i < 3; i++)
		{
			Judge Save;
			if (Flag == 1)
				return;
			switch (i)
			{
			case 0:
				if(j < front.A.length())
				{
					Save.A = front.A.substr(1 + j, front.A.length());
					Save.B = front.B.substr(j, front.B.length());
					Save.num = front.num + 1;
					//cout << i << " 0:" << Save.num << "-" << Save.A << "-" << Save.B << endl;
					store.push(Save);
				}
				break;
			case 1:
				if (j < front.B.length())
				{
					Save.A = front.A.substr(j, front.A.length());
					Save.B = front.B.substr(1 + j, front.B.length());
					Save.num = front.num + 1;
					//cout << i << "1:" << Save.num << "-" << Save.A << "-" << Save.B << endl;
					store.push(Save);
				}
				break;
			case 2:
				if (j < front.B.length() && j < front.A.length())
				{
					Save.A = front.A.substr(1 + j, front.A.length());
					Save.B = front.B.substr(1 + j, front.B.length());
					Save.num = front.num + 1;
					//cout << i << "2:" << Save.num << "-" << Save.A << "-" << Save.B << endl;
					store.push(Save);
				}
				break;
			default:
				break;
			}
		}
		store.pop();
	}
}

int main()
{
	int i, j, N = 0, M;

	Judge Start;
	cin >> Start.A >> Start.B;

	store.push(Start);
	BFS();
	cout << result;
	//cin >> N;
	return 0;
}