问题描述
我刚刚作出了一个队列类,我现在用它来做到这一点。
I have just made a queue class and I now have to use it to do this.
写C ++程序来生成使用A,B,和C字母的所有字符串。
Write a c++ program to generate all strings using A,B,and C as the letters.
的字符串必须按以下顺序产生:一个乙CAAABACBABB公元前CACBCCAAAAABAACABAABBABCACAACBACC等等。
The strings must be generated in the following order:ABCAAABACBABBBCCACBCCAAAAABAACABAABBABCACAACBACCetc.
这应该做到这一点,直到我的队列溢出。
It's supposed to do this until my queue overflows.
现在,我只是不明白老师建议使用的算法,该算法是这样的。
Now, I simply don't understand the algorithm the teacher suggested using, which is this.
开始A和B和C在队列中。删除它显示它,然后添加添加添加
Start with A and B and C in the queue."Remove it Display it then Add Add Add "
该插件添加添加东西抛出我送行,它是如何做到在这个特定的顺序获得这些信吗?
The add add add thing throws me off, how does it accomplish getting these letters in this particular order?
推荐答案
让我们的行为是:
For any token X, add XA, XB, and XC to the queue.
我们的流程是这样的:
Start with a Queue
A B C
Pop (and display) off A
B C
Behave on token: "A"
add AA
add AB
add AC
B C AA AB AC
Pop (and display) off B
C AA AB AC
add BA
add BB
add BC
C AA AB AC BA BB BC
如果我们pretend我们的功能是
If we pretend our function is
main() {
Queue q;
q.add("A");
q.add("B");
q.add("C");
while(true) {
process(q.pop());
}
}
process(String x, Queue q) {
display x;
q.add(x + "A");
q.add(x + "B");
q.add(x + "C");
}
为您找到了吗?
Get it now?
这篇关于帮我理解这种算法(简单)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!