Anagrams by Stack(堆栈字迷)

Time limit: 3.000 seconds
限时:3.000秒

 Link:点击打开链接

Background
背景

How can anagrams result from sequences of stack operations? There are two sequences of stack operators which can convert "TROT" to "TORT":
怎样才能通过一系列的堆栈操作,将字符串按要求的顺序重新排列?比如有两种进出栈序列可以将字符串“TROT”转换为“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.
其中i表示压入,o表示弹出。给定一对单词,你要写一个程序通过一组进出栈的操作,使前一个单词的字母顺序变成后一个单词。

 

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.
输入由多行组成,每对单词占2行。第1行为原始单词(不包括行尾的换行符),第2行为目标单词(同样不包括行尾的换行符)。

 

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.
序列中每组操作应该按字典顺序输出。在一组操作中,i和o用空格隔开,每一组独占一行。

 

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”来表示进栈,“o”表示出栈,栈的初始状态为空。给定一个输入的单词,只要对每一个字母都进行了进栈和出栈操作,这一组操作才可能是有效的。进一步来说,不能对空栈执行弹出操作。比如对于输入的单词FOO,下面的操作序列:

  • i i o i o o is valid, but
    i i o i o o是有效的,但
  • i i o is not (it's too short), 
    i i o是无效的,(没有完成操作)
  • neither is i i o o o i (there's an illegal pop of an empty stack) 
    i i o o o i同样无效(执行了非法的从空栈弹出的操作)

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.
可能有多组有效操作都能够为输入的单词生成指定的字母排列。比如对于输入的单词“FOO”,操作i i o i o o就可以生成重排的单词“OOF”。操作i i i o o o也可以生成同样的重排。你要写一个程序,找出所有有效的操作序列,以使输入的一对单词的前者重排为后者。

 

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
]

 

Analysis
分析

这道题必须遍例所有可能的进出栈序列才能求得全部解。如果将进出栈序列视为二叉树,那么求解的过程就是一个典型的深度遍例。进栈为左子节点,出栈为右子节点。当所有字符都已出栈,即遍例到了叶子节点,应打印输出前面的进出栈序列(也可以视为从根到叶子的路径)。如果在弹栈时发现当前弹出的字符与目标串中的该字符位置不符,则无需再遍例这一支的子节点,直接回溯即可。思路很简单,就看实现了。注意,不要在行尾输出空格。

 

Solution
解答

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <iostream>
#include <string>
#include <vector>
using  namespace  std;
//深度遍例所有可能的进出栈序列,进栈视为左子树,出栈视为右子树
void  GenAnagram(vector< char > &Src, vector< char > &Dest,
                 vector< char > &Order, vector< char > &Stack) {
     //如果字符串不为空,则执行进栈操作,相当于遍例左子树
     if  (!Src.empty()) {
         //在操作序列中加入i
         Order.push_back( 'i' );
         //进栈并保存现场
         Stack.push_back(Src.back());
         Src.pop_back();
         //以当前状态遍例下一个动作
         GenAnagram(Src, Dest, Order, Stack);
         //恢复现场
         Src.push_back(Stack.back());
         Stack.pop_back();
         Order.pop_back();
     }
     //如果栈不为空则执行弹栈操作,当栈顶和目标串相应字符相等时才继续
     if  (!Stack.empty() && Stack.back() == Dest[Stack.size() + Src.size() - 1]) {
         //在操作序列中加入o
         Order.push_back( 'o' );
         //弹栈并保存现场
         char  cTemp = Stack.back();
         Stack.pop_back();
         //以当前状态遍例下一个动作
         GenAnagram(Src, Dest, Order, Stack);
         //恢复现场
         Stack.push_back(cTemp);
         Order.pop_back();
     }
     //如果原字符串已空,且栈也为同,则说明所有字符都已出栈
     if  (Src.empty() && Stack.empty()) {
         //输出进出栈操作序列
         vector< char >::iterator i = Order.begin();
         for  (; i != Order.end() - 1; ++i ) {
             cout << *i << ' ' ;
         }
         cout << *i << endl;
     }
}
//主函数
int  main( void ) {
     //循环处理输入的每一对字符串
     for  (string str1, str2; cin >> str1 >> str2; cout << ']'  << endl) {
         cout << '['  << endl;
         //当两字符串长度相等且不为空时才运算
         if  (str1.length() == str2.length() && !str1.empty()) {
             //建立原串、目标串、字符栈和结果数组
             vector< char > Src(str1.rbegin(), str1.rend());
             vector< char > Dest(str2.rbegin(), str2.rend());
             vector< char > Order, Stack;
             //执行深度遍例,输出所有结果
             GenAnagram(Src, Dest, Order, Stack);
         }
     }
     return  0;
}

### 关于 `Group Anagrams` 的实现方法 #### 方法一:基于排序的哈希表 对于每一个字符串,先将其字符按照字典顺序排列得到一个新的字符串作为键值存入哈希表中。由于字母异位词经过这样的处理后会产生相同的键值,因此可以将具有相同键值的所有原始字符串收集起来形成最终的结果集[^1]。 ```cpp vector<vector<string>> groupAnagrams(vector<string>& strs) { unordered_map<string, vector<string>> mp; for (string& str : strs) { string key = str; sort(key.begin(), key.end()); mp[key].push_back(str); } vector<vector<string>> ans; for (auto it = mp.begin(); it != mp.end(); ++it) { ans.push_back(it->second); } return ans; } ``` 这种方法的时间复杂度主要取决于排序操作以及遍历输入列表的操作。如果n表示输入列表长度,k代表最长单词长度,则时间复杂度大约为O(n * k log k)。 #### 方法二:基于计数数组的哈希表 考虑到ASCII码范围内的字符数量有限(共26个小写字母),可以直接利用固定大小(26)的整型数组记录每个字符串内各个字符出现次数的情况,并以此构建唯一的key用于存储到hashmap当中去。这种方式避免了显式的字符串排序过程,在某些情况下可能会更高效一些。 ```cpp vector<vector<string>> groupAnagrams(vector<string>& strs) { unordered_map<string, vector<string>> mp; for (const auto& s : strs){ array<int, 26> counts{}; for(const char c:s){ counts[c-'a']++; } // 将counts转换成可作为key使用的字符串形式 string key = ""; for(auto count:counts){ key += "#"+to_string(count); } mp[key].emplace_back(s); } vector<vector<string>> res; for(auto& p:mp){ res.emplace_back(p.second); } return res; } ``` 此版本通过使用定长的频率分布向量代替直接对字符串进行排序的方式减少了不必要的计算开销,理论上能够提供更好的性能表现。 #### 方法三:质数相乘法 另一种巧妙的做法是给每个不同的字母分配一个独一无二的小质数值,之后把整个字符串看作是由这些质因数构成的大合数;这样只要两个串对应的积相等就说明它们互为变位词。不过这种思路虽然新颖有趣但在实际应用时效率未必占优,因为涉及到大数运算等问题。 ```cpp // 这里仅给出概念性的伪代码示意而非完整的决方案 unordered_map<long long,vector<string>> map; for each word w do{ product=1; foreach character ch in w do{ product *= primes[ch]; //假设primes[]已经预先定义好并初始化完毕 } map[product].add(w); } return convert(map.values()); // 把map里的value部分转成题目要求的形式返回 ``` 上述三种方式各有特点,可以根据具体场景和个人偏好选择合适的一种来决问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值