string morphing 巧妙dp+dfs

题目大意就是给定两个char类型的组合方式,问怎么组合转化可以让原始串转变成目标串(一个char型字符),并且输出中间转化过程。

整体思路:就是纯暴力。中间一定要记忆化一下。

There is a special multiplication operator such that
Table of special multiplication operation
Thus
ab
=
b
,
ba
=
c
,
bc
=
a
,
cb
=
c
, . . .
For example, you are given the string
bbbba
and the character
a
,
(b(bb))(ba) = (bb)(ba)  [as bb = b]
= b(ba)     [as bb = b]
= bc
[as ba = c]
= a
[as bc = a]
By adding suitable brackets,
bbbba
can produce
a
according to the above multiplication table.
You are asked to
write  a  program
to show the morphing steps of a string into an expected
character, or otherwise, output `
None exist!
' if the given string cannot be morphed as expected.
Input
The  rst line of the input  le gives the number of test cases. Each case consists of two lines. The  rst
line is the starting string which has at most 100 characters. The second line is the target character.
All characters in the input are within the range of
a
-
c
.
Output
For each test case, your output should consist several lines, showing the morphing steps of a string into
the character. In case there are more than one solution, always try to start the morphing from the left.
Print a blank line between consecutive sets of output.
Sample Input
2
bbbba
a
bbbba
a
Sample Output
bbbba
bbba
bba
bc
a
bbbba
bbba
bba
bc
a

代码:

#include<bits/stdc++.h>
using namespace std;
int t;
string h1,h2;
map<string ,string >haha;
char change(char a,char b)
{
    if(a=='a')
    {
        if(b=='a'||b=='b')
            return 'b';
        else if(b=='c')
            return 'a';
    }
    else if(a=='b')
    {
        if(b=='a')
            return 'c';
        else if(b=='b')
            return 'b';
        else if(b=='c')
            return 'a';
    }
    else if(a=='c')
    {
        if(b=='a')
            return 'a';
        else if(b=='b'||b=='c')
            return 'c';
    }
}


int dfs(string h)
{
    if(h.length()==1)
    {
        return h==h2;
    }
    string xpp="";
    for(int i=1;i<h.length();i++){
            xpp="";
        for(int j=0;j<i-1;j++){
            xpp=xpp+h[j];
        }
        xpp=xpp+change(h[i-1],h[i]);
        for(int j=i+1;j<h.length();j++){
            xpp=xpp+h[j];
        }
        if(haha.count(xpp)!=0)///这种状态已经找过了
            continue;
        haha[xpp]=h;///表明xpp的上一个状态是h
       // cout<<"^^&^"<<endl;
        if(dfs(xpp)==1)
            return 1;
    }
    return 0;
}
void digui(string h)
{
    if(h==h1)
    {
        cout<<h1<<endl;
        return ;
    }
    digui(haha[h]);
    cout<<h<<endl;
}
int main()
{
    scanf("%d",&t);
    while(t--)
    {
        cin>>h1>>h2;
        haha.clear();
        int L=dfs(h1);
        if(L==1)
        {
            digui(h2);
        }
        else{
            cout<<"None exist!"<<endl;
        }
        if(t!=0)
        cout<<endl;
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值