hdu1515  Anagrams by Stack

來源:互聯網
上載者:User
Problem DescriptionHow 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.
 


InputThe 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. 


OutputFor 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. 


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#include#include#include#includeusing namespace std;char str1[300],str2[300],str3[300],str4[300],strtemp[300];int visit[300];stack q;int m,temp,flag2,flag3;char pri;bool find(int n,int in ,int out){if((n==2*m)&&(in==m)&&(out==m)){printf("i");for(int i=1;iprintf(" %c",str3[i]);printf(" \n");return true;}else if(n==2*m)return false;if(in{str3[n]='i';q.push(str1[flag2]);flag2++;find(n+1,in+1,out);q.pop();flag2--;
}if(out{str3[n]='o';if(q.empty()){
return false;}char strchar=q.top();if(strchar!=str2[out])return false;
strtemp[flag3++]=q.top();// printf("%c %d ",strchar,out);q.pop();find(n+1,in,out+1);q.push(strchar);flag3--;}return true;}int main (){int i,j;memset(str1,'\0',sizeof(str1));memset(str2,'\0',sizeof(str2));memset(str3,'\0',sizeof(str3));memset(str4,'\0',sizeof(str4));memset(strtemp,'\0',sizeof(strtemp));while(gets(str1)!=NULL){gets(str2);
for(i=0;str1[i]!='\0';i++){}for(j=0;str2[j]!='\0';j++){}if(i!=j){printf("[\n]\n");continue;}m=i;visit[0]=1;printf("[\n");str3[0]='i';flag2=0;flag3=0;while(!q.empty()){q.pop();}q.push(str1[flag2]);flag2++;find(1,1,0);printf("]\n");memset(str1,'\0',sizeof(str1));memset(str2,'\0',sizeof(str2));memset(str3,'\0',sizeof(str3));memset(str4,'\0',sizeof(str4));memset(strtemp,'\0',sizeof(strtemp));}
return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.