帮我理解这种算法(简单) [英] Help me understand this algorithm (simple)

查看:152
本文介绍了帮我理解这种算法(简单)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚作出了一个队列类,我现在用它来做到这一点。

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.

的字符串必须按以下顺序产生: 一个 乙 C AA AB AC BA BB 公元前 CA CB CC AAA AAB AAC ABA ABB ABC ACA ACB ACC 等等。

The strings must be generated in the following order: A B C AA AB AC BA BB BC CA CB CC AAA AAB AAC ABA ABB ABC ACA ACB ACC etc.

这应该做到这一点,直到我的队列溢出。

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?

这篇关于帮我理解这种算法(简单)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆