Anagrams by Stack
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 614 Accepted Submission(s): 311
Description
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.
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.
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
]
Source
HDU1515ZOJ1004
寒假刚开始做ACM的时候做了这道题,还是难得做出来的一道,只知道用递归做,到最后是做出来了,但却发现怎么顺序刚好完全相反,汗,于是只好将结果都用vector储存起来,然后再reverse一遍,提交,祈祷通过,结果还真通过了,然后就心满意足地去睡觉了 [流汗]
当时的代码(在ZOJ可以AC,HDU里TLE,只作留念,写的很冗长):
#include <iostream>
#include <string>
#include <iterator>
#include <vector>
#include <algorithm>
using namespace std;
void Solve(string wordA, string wordB, vector<string> &solutions, string resultA = "", string resultB = "", string solution = ""){
if(wordA.length() != wordB.length())
return;
if(solution.length() == wordB.length() * 4){
if(resultB == wordB)
solutions.push_back(solution);
//cout << 's' << solution << endl;
//else
//cout << 'f' << solution << ends << resultB << endl;
return;
}
if(resultA.length() > 0){
char c = *(resultA.end() - 1);
resultA.erase(resultA.end() - 1);
resultB.push_back(c);
solution += "o ";
Solve(wordA, wordB, solutions, resultA, resultB, solution);
resultA.push_back(c);
resultB.erase(resultB.end() - 1);
solution.erase(solution.end() - 1);
solution.erase(solution.end() - 1);
}
if(resultA.length() + resultB.length() < wordA.length()){
char c = wordA.at(resultA.length() + resultB.length());
resultA.push_back(c);
solution += "i ";
Solve(wordA, wordB, solutions, resultA, resultB, solution);
}
}
int main(){
string wordA, wordB;
while(cin >> wordA >> wordB){
cout << '[' << endl;
vector<string> solutions;
Solve(wordA, wordB, solutions);
reverse(solutions.begin(), solutions.end());
for(vector<string>::iterator iter = solutions.begin(); iter != solutions.end(); ++iter)
cout << *iter << endl;
cout << "]" << endl;
}
return 0;
}
现在的代码:
#include<iostream>
#include<string>
using namespace std;
//int count; //当时用来检测剪枝是否完全用的计数器
void BackTrake(string &s1, string &s2, string s = "", string e = "", string method = ""){ //s1原串,s2结果,s字符进栈储存,e字符出栈储存
if(e.length() == s2.length()){
cout << method << endl;
return;
}
//++count;
if(s.length() + e.length() < s1.length()) //如果已经拿出来的字符数的长度小于原串的长度,即还可以继续进栈
BackTrake(s1, s2, s + s1[s.length() + e.length()], e, method + "i "); //模拟进栈,进行下层递归
if(s.length() > 0 && s2[e.length()] == s[s.length() - 1]){ //如果还有字符可以出栈,并且下一个字符与s串最后一个字符相同
e += s[s.length() - 1]; //模拟出栈
s.erase(s.end() - 1);
BackTrake(s1, s2, s, e, method + "o "); //下层递归
}
}
int main(){
string s1, s2;
while(cin >> s1 >> s2){
//count = 0;
cout << "[" << endl;
if(s1.length() == s2.length())
BackTrake(s1, s2);
cout << "]" << endl;
//cout << count << endl;
}
return 0;
}
=======================签 名 档=======================
原文地址(我的博客):http://www.clanfei.com/2012/05/1098.html
欢迎访问交流,至于我为什么要多弄一个博客,因为我热爱前端,热爱网页,我更希望有一个更加自由、真正属于我自己的小站,或许并不是那么有名气,但至少能够让我为了它而加倍努力。。
=======================签 名 档=======================
分享到:
相关推荐
杭电hdu acm资料所用杭电的acm题
杭电 hdu acm 第1084题的解法,ac过了,是一位学长教我的.内有一些中文说明.
写一个数据结构算法,杭电上一道题目,有关数据结构的题目。
For a positive integer n, let’s denote function f(n,m) as the m-th smallest integer x that x>n and gcd(x,n)=1. For example, f(5,1)=6 and f(5,5)=11. You are given the value of m and (f(n,m)?...
此程序为hdu的acm2010题,就是解决水仙花数问题
acm经典题、结题报告,对初学者非常用用,我就从中学到了很多东西
ACM比赛解题报告,包括hdu1880、zoj1010、zoj1015,为原创的报告,算法不一定最优的
杭电acm解题报告 详细解析2000-2099 适合acm初学者
杭电acm 一些java语言实现的水题目
杭电的一些acm题目,都是我自己一个一个自己提交的
ACM_算法模板集史上最完整收藏版223页全免费版.pd
ACM_HDU 我的 acm 问题的一部分在 hdoj 中解决了。
HDU_ACM_1002_大数相加C源代码,利用字符串处理
noi试题和解析,对于参加acm非常有帮助
浙江大学ACM题解JU_ACM_All_Anwer,里面一本非常好的chm电子书,浙大的所有ACM题及题解都在了,对学习ACM的朋友非常的好~还等什么呢?
ACM_基础篇
Pascal acm_timus_ural_1148.pas
Pascal acm_timus_ural_1099.pas
ACM__ICPC__重要补充知识.doc
ACM,竞赛题目,我已经在上面测试过了,可以用,题目是ARGUS,希望大家可以喜欢,花了长时间才弄好的