1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > Anagrams by Stack | Python 实现

Anagrams by Stack | Python 实现

时间:2019-01-20 08:01:35

相关推荐

Anagrams by Stack | Python 实现

目录

1.题面

2.注意事项:

0.OJ平台

1.无限流输入 、EOF输入流

2.返回中的空格

3.AC代码

1.题面

Anagrams by Stack

Time Limit: 2000 msMemory Limit: 65536 KB

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 oi o i i o o i o]

whereistands for Push andostands 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 ofiandowhich 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, eachiandois 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 symboli(in) for push ando(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:

Valid sequences yield rearrangements of the letters in an input word. For example, the input word FOO and the sequencei i o i o oproduce the anagram OOF. So also would the sequencei i i o o o. You are to write a program to input pairs of words and output all the valid sequences ofiandowhich will produce the second member of each pair from the first.

Sample Input

madamadammbahamabahamalongshortericrice

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 ]

大概意思就是每两个输入为一组,第一个字符变换到第二个字符的堆栈结构变化一共有多少种可能。用'i','o'这样子的结构来表示。最后这多种结果用字典序排序后输出。

知识点是dfs,别往难的地方猜,你写暴力的dfs搜索就能过,至于Python的小细节,这里给出注意事项:

2.注意事项:

0.OJ平台

该题目的url:ZOJ/problem-sets/91827364500/problems/91827364503我是从清化大学叫兽们出的书中看到的这道题目,原来给的链接已经没了,然后发现还好心给了提示,好多CSDN博客还在给那个链接:http://acm./onlinejudge/showProblem.do?problemCode=1004,看了看还是的博客,害,古老的oj和远古的大佬,好吧好吧。

所以这里稍微提醒一下 这些题目已经转移到了 PTA 平台 中去了,搜拼题A,可以搜到,再找找ZOJ,就能找到了。(或者上边已经给了可用链接直接点也可以)

只有找到题目才能愉快地提交代码嘛,才能有进步。

1.无限流输入 、EOF输入流

C、C++可以使用while (scanf("%s",a)),或者while (cin>>a>>b),到文件末尾自动结束,但是我们Python的宝藏选手们可纠结了,你说牛客还支持放一个无限while挂着都没问题,这里的OJ你用正常读法会变成返回非零:Non Zero Exit Error。

因此给出正确写法:

while 1:#True 也行try:s1 = input()s2 = input()except EOFError:break

这样你才不会卡在第一个容易报错的地方。

2.返回中的空格

如果存的结果是"iioioo"直接输出的话,报错是:Presentation Error,原来是漏掉了空格。

那我写成:

' '.join('iioioo')

#输出应该是:i i o i o o

但是问题还是报错,一样的原因。

然后我就试着鼠标扫了一下空格,发现。。。。。。。这样还不行,最后面tmd还有空格,这不应该是各大平台已经极力禁止的操作了吗?算了算了,谅它还是个不成熟的OJ题目(毕竟挺早的了),原谅它吧。

正确应该写成:

' '.join('iioioo')+' '

如此,大功告成。

3.AC代码

def dfs(s1,s2,index,ans_s):if len(s2) == len(target):#如果长度相同并且拼凑出了target字符,即s2 == target,答案数组增加s2这个答案字符if s2 == target:ans.append(ans_s)returnif index == len(s):#如果长度和 s 相同,要么代表栈里的s1没了,没有答案,要么继续把s1的字符一个一个弹出if s1 == '':returndfs(s1[:-1],s2+s1[-1],index,ans_s+'o')returnif s1 == '':#s1空了,但是还可以入栈的情况dfs(s1+s[index],s2,index+1,ans_s+'i')return#均不属于以上情况,存在两种操作:入栈或者弹出dfs(s1[:-1], s2 + s1[-1], index, ans_s + 'o')dfs(s1 + s[index], s2, index + 1, ans_s + 'i')while 1:try:ans = []s = input()target = input()dfs('','',0,'')ans.sort()print('[')for i in ans:print(' '.join(i)+' ')print(']')#一定要EOFError判断,不然会挂except EOFError:break

PS:

这里建议自己写一些,我的代码好乱,本来已经准备好优化一下的,结果一直看不是超时的原因就没改。而且貌似这题十分地单纯,DFS,并不是很难。

写的话我个人认为用字符存储结果能够省下很多空间了,而且模拟弹出栈元素十分方便(利用切片操作)。

优化的话比如每次len()一遍各个长度肯定费时的,有兴趣的兄弟可以在这里早早记录一下,之后调用就好了。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。