P1032 string conversion (BFS)

https://www.luogu.org/problemnew/show/P1032
subject description
are known rule two strings A, BA, B, and a set of string conversion (up to 66 rules):

A 1-> B 1
A 2 -> B 2

Rule as meaning: A substring in A 1
may be converted to B 1, A 2 may be converted to 2 B
....

E.g:A='abcd'B='xyz'

The conversion rule is:

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

At this time, A may be changed through a series of transformations B, which process is converted:

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

Converting a total of three times, such that AA is converted into BB.

Input Output Format
Input Format:
input format is as follows:

AA BB
A 1 B 1	 
A 2 B 2

| -> transformation rules

… … /

The upper limit for the length of the string 20 all.

Output formats:
Output to the screen. Format is as follows:

If in step 10 (step 10 comprises) can Within A conversion is B, then the minimum number of steps transform output; otherwise outputs "NO ANSWER!"

Input Output Sample
Input Sample # 1:

abcd xyz
abc xu
ud y
y yz

Output Sample # 1:

3

ac_code:
/ *
Each point is a string, tag ~ thought map with
prior dfs done with only 60 minutes, and then the first time to do bfs 80 hours, the last set of data WA
points WA is not taken into account for a string a transformation rule may have multiple position variable, but they are a step to get there:
For example:

aacadb   abcdef(原串,目标串)
a  b(其中一条转换规则)
一步就可以得到:
bacadb
abcadb
aacbdb

So for the next rule carried out after the original string is not found a match on the conversion
but continue to look to look at this rule have no other place can match
so to check whether there is when you can use conversion rules to use while
there for input it is uncertain number of rules, you can use ctrl + z ended input , but in the end you want to record how many rules with
* /

#include <iostream>
#include <string>
#include <queue>
#include <map>
using namespace std;
struct rules
{
    string A;
    string B;
    int len_a;
} data[10];
string s,e;
int cnt;
map<string,bool>vis;
map<string,int>step;
int bfs()
{
    queue<string>q;
    q.push(s);
    vis[s] = true;
    while(!q.empty())
    {
        string k = q.front();
        if(step[k] > 10)
        {
            break;
        }
        q.pop();
        if(k == e)
        {
            return step[k];
        }
        for(int i = 0; i < cnt; i++)
        {
            int pos = -1;
            //string t = k; //如果要直接用replace要先记录下原串
            while(k.find(data[i].A,pos+1)!=string::npos)
            {
                pos = k.find(data[i].A,pos+1);
                //cout<<pos<<endl;
                if(pos != -1)
                {
                    string s1 = k.substr(0,pos);
                    int t = pos + data[i].len_a;
                    string s2 = k.substr(t,k.length()-t);
                    string now = s1 + data[i].B + s2;
                    //string now = k.replace(pos,data[i].len_a,data[i].B);//replace会使原串改变
                    //k = t;
                    //cout<<now<<" "<<k<<endl;
                    if(!vis[now])
                    {
                        vis[now] = true;
                        step[now] = step[k] + 1;
                        q.push(now);
                    }
                }
            }
        }
    }
    return -1;
}
int main()
{
    cin>>s>>e;
    for(int i = 0; i < 6; i++)
    {
        cin>>data[i].A>>data[i].B;
        data[i].len_a = data[i].A.length();
        if(data[i].A == "\0")
            break;
        else cnt++;
    }
    int ans = bfs();
    if(ans != -1)
        cout<<ans<<endl;
    else
        cout<<"NO ANSWER!"<<endl;
    return 0;
}


Guess you like

Origin blog.csdn.net/tb_youth/article/details/92388489