【dfs 回溯】 zoj 1004

时间:2023-03-10 03:25:53
【dfs 回溯】 zoj 1004

题意:给出一个源字符串和一个目标字符串,打出所有符合stack操作的i,o串使得对于源字符串的操作能变为目标字符串

思路:搜索,回溯。

之前想过是不是队列,觉得不对那样bfs是求最优解了;也想过用结构体数组来保存访问过的i,o操作序列,也是越写越麻烦,自己把自己绕进去了。

参考了网上的一篇博客,思路特别清晰。

回溯嘛...访问时进入下一个函数循环体,而函数体后认为并没有访问即可

反映到这道题上,如果是入栈操作,在func(s)后怎么入的就怎么弹出来,参数全置到未入栈之前的操作即可,出栈同理。

#include <cstdio>
#include <iostream>
#include <stack>
#include <cstring>
using namespace std;
];//来源字符串
];//目标字符串
];//记录步骤的字符数组
int steplen,sp,tp,n;//sp:字符在sword中的位置,tp:记录字符在tword中的位置
void func(stack&s)
{
    char ch;
    int i;
    if(tp==n)//与目标字符串校对完成,所以step必然是正确的
    {
        ;i
        {
            cout<<step[i]<<" ";
        }
        cout<<endl;
        return;
    }
    if(sp
    {
        s.push(sword[sp++]);
        step[steplen++]='i';
        func(s);
        s.pop();
        steplen--;
        sp--;
    }
    if(!s.empty())//栈s不为空时,对比栈头和当前目标字符
    {              //如果相等,便产生输出o
        ch=s.top();
        if(ch==tword[tp])
        {
            s.pop();
            tp++;
            step[steplen++]='o';
            func(s);
            steplen--;
            tp--;
            s.push(ch);
        }
    }
}
int main()
{
    //freopen("in.txt","r",stdin);
    stack str;
    while(cin >> sword >> tword)
    {
        cout << "[" << endl;
        n=strlen(sword);
        if(n==strlen(tword))
        {
            sp=;
            tp=;
            steplen=;
            func(str);
        }
        cout<<"]"<<endl;
    }
    ;
}