[Luogu P1032] String conversion

Luogu topic link: string transformation

Topic description

It is known that there are two strings A, B and a set of rules for string transformation (up to 6 rules):

A1 -> B1

A2 -> B2

The meaning of the rule is: the substring A1 in A$ can be transformed into B1, A2 can be transformed into B2….

For example: A='abcd'B='xyz'

The transformation rules are:

‘abc’->‘xu’‘ud’->‘y’‘y’->‘yz’

At this time, A can be transformed into B through a series of transformations, and the transformation process is as follows:

‘abcd’->‘xud’->‘xy’->‘xyz’

A total of three transformations are performed, so that A is transformed into B.

Input and output format

Input format:

The input format is as follows:

A B A1 B1 \

A2 B2 |-> Transformation Rules

... ... /

All strings have a maximum length of 20.

Output format:

output to the screen. The format is as follows:

If A can be transformed into B within 10 steps (including 10 steps), output the minimum number of transformation steps; otherwise, output "NO ANSWER!"

Input and output example

Input example #1:

abcd xyz

abc xu

ud y

y yz

Sample output #1:

3

Briefly describe the meaning of the question: give a starting string, a target string, and a bunch of transformation methods. Ask whether the starting string can be converted into a target string within 10 steps.
Obviously, it is a direct wide search or a deep search. Mainly The trouble is dealing with the transformation of this string.

For the sake of convenience, the string type that comes with STL is used here. Learn from herecopyI took a look at Luogu's problem solution and learned the usage of string class functions.
Let's talk about string class functions.

  • define a string type

  • Assign a value to a string type (the subscript starts at 0).

  • Add a character after a string type.

  • length() returns the length of a string type.

  • begin()/end() returns an iterator pointing to the first/last element of the string.

  • find() finds the first occurrence of a subscript of a string in a string type.

  • erase() deletes a substring in a string type (two parameters are index and delete length).

  • substr() finds a string of type string.

      string s;
    s = "this_is_string";//对string类型赋值
    int len = s.length();//返回string类型的长度
    printf("len=%d\n",len);
    s += "_haha";//在string类型最后加上一个字符串
    string::iterator it;
    string::iterator start = s.begin();
    string::iterator end = s.end();//定义一个string类型的迭代器
    for(it = start;it != end;it++)
      	printf("%c",*it);
    printf("\n");
    int pos = s.find("is");//查找string类型中一个字符串第一个字母第一次出现的位置
    cout << pos << endl;
    s.erase(s.find("_"),3);
    //s.erase(start+8,end-5);//同样也是删除
    cout << s.substr() << endl;//默认直接输出整个string
    cout << s.substr(5) << endl;//从第5位截取到最后
    cout << s.substr(5,6) << endl;//从第12位开始截取4的长度的字串
    

In fact, I don't think this problem is very difficult, but the operation of this conversion character is more troublesome, so the main thing is to write the function of this conversion character.

#include<bits/stdc++.h>
using namespace std;

int cnt = 0;
string st, ed;
string c1[10], c2[10];

int step[100000];
string q[100000];
map <string,bool> vis;

string change(string s,int i,int j){
    string ans = "";
    if (i+c1[j].length() > s.length())
        return ans;
    for (int k=0; k < c1[j].length();k++)
        if (s[i+k] != c1[j][k])
            return ans;
    ans = s.substr(0,i);
    ans += c2[j];
    ans += s.substr(i+c1[j].length());
    return ans;
}

void bfs(){
    int head = 1, tail = 1, len, x , flag = 0;
    string now, nx;
    q[tail] = st; vis[st] = 1;
    while(head <= tail){
	    x = head; head++;
	    now = q[x]; len = now.length();
	    if(now == ed){flag = 1; break;}
	    for(int i=0;i<len;i++)
	        for(int j=1;j<=cnt;j++){//枚举每一位进行修改
		        nx = change(now,i,j);
		        if(vis[nx]) continue;
		        q[++tail] = nx; step[tail] = step[x]+1;
		        vis[nx] = 1;
	        }
    }
    if(flag && step[x] <= 10) printf("%d\n",step[x]);
    else printf("NO ANSWER!\n");
}

int main(){
    //freopen("data.in","r",stdin);
    cin >> st >> ed;
    while(cin >> c1[++cnt]) cin >> c2[cnt];
    cnt--;
    bfs();
    return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324970332&siteId=291194637