luogu P1032 字串变换

题目描述

已知有两个字串 A, B 及一组字串变换的规则(至多6个规则):

     A1 -> B1

     A2 -> B2

规则的含义为:在 A$中的子串 A1 可以变换为 B1、A2 可以变换为 B2 …。

例如:A='abcd'B='xyz'

变换规则为:

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

则此时,A 可以经过一系列的变换变为 B,其变换的过程为:

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

共进行了三次变换,使得 A 变换为B。

输入输出格式

输入格式:

 

键盘输人文件名。文件格式如下:

A B A1 B1 \

   A2 B2 |-> 变换规则

... ... /

所有字符串长度的上限为 20。

 

输出格式:

 

输出至屏幕。格式如下:

若在 10 步(包含 10步)以内能将 A 变换为 B ,则输出最少的变换步数;否则输出"NO ANSWER!"

 

输入输出样例

输入样例#1:
abcd xyz
abc xu
ud y
y yz
输出样例#1:
3

貌似有点偏离题意,最后一点过不了
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <queue>
#include <map>

using namespace std;

struct Node{
    string str;
    int tot;
};
Node ST, topp;
int n = 1, js, len_from[21];
string s1, s2, now_st, nxt;
string from[10], to[10];
queue <Node> Q;
map <string, bool> mp;

inline bool pd_out(string s)
{
    if(mp[s])
        return 0;
    else
        mp[s] = 1;
    return 1;
}

inline void pd_answer(string s, int answer)
{
    if(s == s2)
    {
        printf("%d", answer);
        exit(0);
    }
}

inline void  bfs()
{
    ST.str = s1;
    ST.tot = 0;
    Q.push(ST);
    while(!Q.empty())
    {
        topp = Q.front();
        //cout<<"topp:"<<topp<<endl;
        Q.pop();
        //if(topp.tot == 11)
            //return ;
        string topp_st = topp.str;
        for(int i = 1; i <= n; i ++)
        {
            now_st = from[i];
            int Find = topp_st.find(now_st);
            if(Find != -1)
            {
                nxt.clear();
                for(int j = 0; j < Find; j ++)
                    nxt += topp_st[j];
                nxt += to[i];
                int len = len_from[i];
                int now_len = topp_st.length();
                for(int j = Find + len_from[i]; j <= now_len - 1; j++)
                    nxt += topp_st[j];
                pd_answer(nxt, topp.tot + 1);
                if(pd_out(nxt))
                {
                    Node nxt_in;
                    nxt_in.str = nxt;
                    nxt_in.tot = topp.tot + 1;
                    Q.push(nxt_in);
                }
            }
        }
    }
}

int main()
{
    cin >> s1 >> s2;
    while(cin >> from[n] >> to[n])
    {
        len_from[n] = from[n].length();
        n ++;    
    } 
    n --;
    bfs();
    printf("NO ANSWER!");
    return 0;
}
/*
abaaaba abcdaba
a b
b d
d e
e f
f g
g c


*/

有一个函数substr

#include <cstdio>
#include <iostream>
#include <string>
#include <queue>
#include <set>
#include <cstring>

using namespace std;
string a, b;

struct node{
    string fr,to;
}s[20];
int ind=1;
struct po
{
    string pr;
    int t;
    po(string tmp,int te)
    {
        pr=tmp;
        t=te;
    }
};
queue<po> que;
set<string> ss;
int main()
{
    for(int i = 1; i <= -121212; i ++)
        cout<<"dshafjdsfsdfasdfadfas";
    cin>>a>>b;
    while(cin>>s[ind].fr>>s[ind].to) ind++;
    ind--;
    que.push(po(a,0));
    ss.insert(a);
    while(!que.empty())
    {
        string tmp=que.front().pr;
        int t=que.front().t;
        que.pop();
        if(t>10) continue;
        if(tmp==b)
        {
            cout<<t<<endl;
            return 0;
        }
        for(int i=1;i<=ind;i++)
        {
            for(int j=0;j<=int(tmp.size())-int(s[i].fr.size());j++)
            {
                if(tmp.substr(j,s[i].fr.size())==s[i].fr)
                {
                    string pp=tmp.substr(0,j)+s[i].to;
                    if(j+s[i].fr.size()<tmp.size())
                        pp+=tmp.substr(j+s[i].fr.size());
                    if(ss.find(pp)==ss.end())
                    {
                        ss.insert(pp);
                        que.push(po(pp,t+1));
                    }
                }
            }
        }
    }
    printf("NO ANSWER!");
  return 0; }

 

posted @ 2017-10-15 15:41  ioioioioioio  阅读(219)  评论(0编辑  收藏  举报