您的位置:首页 > 大数据 > 人工智能

zoj 1004 Anagrams by Stack (回溯)

2011-06-03 10:13 441 查看
开始觉得无从下手啊 毕竟自己是个菜鸟....

用递归,回溯法

程序写的有点乱,主要是变量命名时没有什么规则,让人看不懂,自己也懒得修改了, 注释也有点不清楚......

原题:

How can anagrams result from sequences of stack operations? There are two sequences of stack operators which can convert TROT to TORT:

[
i i i i o o o o
i o i i o o i o
]

where i stands for Push and o stands for Pop. Your program should, given pairs of words produce sequences of stack operations which convert the first word to the second.

Input

The input will consist of several lines of input. The first line of each pair of input lines is to be considered as a source word (which does not include the end-of-line character). The second line (again, not including the end-of-line character) of each pair is a target word. The end of input is marked by end of file.

Output

For each input pair, your program should produce a sorted list of valid sequences of i and o which produce the target word from the source word. Each list should be delimited by

[
]

and the sequences should be printed in "dictionary order". Within each sequence, each i and o is followed by a single space and each sequence is terminated by a new line.

Process

A stack is a data storage and retrieval structure permitting two operations:

Push - to insert an item and
Pop - to retrieve the most recently pushed item
We will use the symbol i (in) for push and o (out) for pop operations for an initially empty stack of characters. Given an input word, some sequences of push and pop operations are valid in that every character of the word is both pushed and popped, and furthermore, no attempt is ever made to pop the empty stack. For example, if the word FOO is input, then the sequence:

i i o i o ois valid, but
i i o is not (it's too short), neither is
i i o o o i(there's an illegal pop of an empty stack)
Valid sequences yield rearrangements of the letters in an input word. For example, the input word FOO and the sequence i i o i o o produce the anagram OOF. So also would the sequence i i i o o o. You are to write a program to input pairs of words and output all the valid sequences of i and o which will produce the second member of each pair from the first.

Sample Input

madam
adamm
bahama
bahama
long
short
eric
rice


Sample Output

[
i i i i o o o i o o
i i i i o o o o i o
i i o i o i o i o o
i i o i o i o o i o
]
[
i o i i i o o i i o o o
i o i i i o o o i o i o
i o i o i o i i i o o o
i o i o i o i o i o i o
]
[ ][
i i o i o i o o
]

程序:


#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
#define N 20
char str
;
char  res
;
int  cur,s_top,id;                     //cur指向源字符串str 表示cur之前的已经入栈了 s_top是栈的头指针,id是 state的头指针
int len;                                   //字符串长度
int len2;
int cur_res;                             //指向res字符串的当前位置,用于和出栈的字符比较是否相等
void solve();
char stack
;                         //栈
char  state[2*N];                     //存放 i 和o的数组
char  p
;
void print_ans();
int main()
{
freopen("in.txt","r",stdin);
while(scanf("%s",str)!=EOF)
{
cur=s_top=id=0;
cur_res=0;
len=strlen(str);
len2=2*len;
scanf("%s",res);
int l2=strlen(res);
if(l2!=len)
{
cout<<'['<<endl;
cout<<']'<<endl;
continue;
}
cout<<'['<<endl;
solve();
cout<<']'<<endl;
}
}

void solve()
{
if(id==len2)
{
print_ans();
return ;
}
if(cur<len)                         //cur<len代表源字符串还有没进栈的,否自都进栈了
{
state[id++]='i';
stack[s_top++]=str[cur];
cur++;
solve();
cur--;
id--;
s_top--;
}
if(s_top>0)                        //表示还有没出栈的  可以出栈
{
char tmp=stack[s_top-1];                     //保存 要删除(出栈)的元素 ,以便恢复
// cout<<res[cur_res]<<endl;
if(res[cur_res]!=tmp)                             //即出栈的元素和所要的结果不同,就返回,不需要再处理了
return;
cur_res++;
state[id++]='o';
s_top--;
solve();
stack[s_top++]=tmp;
cur_res--;
id--;
}
}

void print_ans()
{
for(int i=0;i<len2;i++)
{
cout<<state[i]<<' ';
}
cout<<endl;
return ;
}


 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息