首页 > 搜索 > DFS搜索 > HDU 1515 Anagrams by Stack-栈-[解题报告] C++
2013
12-12

HDU 1515 Anagrams by Stack-栈-[解题报告] C++

Anagrams by Stack

问题描述 :

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.

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 o is 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.

输入:

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.

输出:

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.

样例输入:

madam
adamm
bahama
bahama
long
short
eric
rice

样例输出:

[
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 
]

深搜,有点难

参考:http://zc634579757.blog.163.com/blog/static/1244974622009829263133/

 

#include "stdio.h"
#include "string.h"

#define M 100

char source[M], target[M];
int len;
char seq[2*M];
int p;

int stack[M];
int top;

void DFS(int ls, int lt){
	int i;
	char c;
	if(lt == len){
		for(i=0; i<2*len; i++)
			printf("%c ", seq[i]);
		printf("\n");
		return;
	}
	if(ls < len){
		seq[p++] = 'i';
		stack[top++] = source[ls];
		DFS(ls+1, lt);
		top--;
		p--;
	}
	if(top && stack[top-1] == target[lt]){
		seq[p++] = 'o';
		c = stack[--top];
		DFS(ls, lt+1);
		p--;
		stack[top++] = c;
	}
}

void main(){
	freopen("in.txt", "r", stdin);
	while(gets(source)){
		gets(target);
		printf("[\n");
		if((len = strlen(source)) == (int)strlen(target)){
			p = 0;
			top = 0;
			DFS(0, 0);
		}
		printf("]\n");
	}
}

 

madam

adamm

 

ls指向source串,lt指向target串。建立一个stack,用来模拟进栈出栈。seq用来保存进出记录。

1、当ls指向的字符不等于lt指向的字符(下面直接简写为ls不等于lt)时,ls进栈

2、当ls等于lt时,分两步:将ls后面继续进栈;将ls弹出栈,lt++

解题报告转自:http://blog.csdn.net/chaoojie/article/details/7571178


  1. bottes vernies blanches

    I appreciate the efforts you men and women place in to share blogs on such sort of matters, it was certainly useful. Keep Posting!

  2. #include <cstdio>
    #include <cstring>

    const int MAXSIZE=256;
    //char store[MAXSIZE];
    char str1[MAXSIZE];
    /*
    void init(char *store) {
    int i;
    store['A']=’V', store['B']=’W',store['C']=’X',store['D']=’Y',store['E']=’Z';
    for(i=’F';i<=’Z';++i) store =i-5;
    }
    */
    int main() {
    //freopen("input.txt","r",stdin);
    //init(store);
    char *p;
    while(fgets(str1,MAXSIZE,stdin) && strcmp(str1,"STARTn")==0) {
    if(p=fgets(str1,MAXSIZE,stdin)) {
    for(;*p;++p) {
    //*p=store[*p]
    if(*p<’A’ || *p>’Z') continue;
    if(*p>’E') *p=*p-5;
    else *p=*p+21;
    }
    printf("%s",str1);
    }
    fgets(str1,MAXSIZE,stdin);
    }
    return 0;
    }