【发布时间】:2012-05-26 13:00:06
【问题描述】:
我正在为周一的面试做准备,我发现这个问题需要解决,称为“String Reduction”。问题是这样表述的:
给定一个由 a、b 和 c 组成的字符串,我们可以执行以下操作 操作:取任意两个相邻的不同字符并替换它 与第三个字符。例如,如果 'a' 和 'c' 相邻, 他们可以用'b'代替。最小的字符串是多少 反复应用这个操作的结果?
例如 cab -> cc 或 cab -> bb,产生一个长度为的字符串 2. 对于这个,一个最优解是:bcab -> aab -> ac -> b。无法应用更多操作,结果字符串的长度为 1。 如果字符串为 = CCCCC,则无法执行任何操作,因此 答案是 5。
我在 stackoverflow 上看到了很多 questions and answers,但我想验证我自己的算法。这是我的伪代码算法。在我的代码中
- S 是我要减少的字符串
- S[i] 是索引 i 处的字符
- P 是一个堆栈:
-
redux 是减少字符的函数。
function reduction(S[1..n]){ P = create_empty_stack(); for i = 1 to n do car = S[i]; while (notEmpty(P)) do head = peek(p); if( head == car) break; else { popped = pop(P); car = redux (car, popped); } done push(car) done return size(P)}
我的算法的最坏情况是 O(n),因为堆栈 P 上的所有操作都在 O(1) 上。我在上面的例子中尝试了这个算法,我得到了预期的答案。 让我用这个例子“abacbcaa”执行我的算法:
i = 1 :
car = S[i] = a, P = {∅}
P is empty, P = P U {car} -> P = {a}
i = 2 :
car = S[i] = b, P = {a}
P is not empty :
head = a
head != car ->
popped = Pop(P) = a
car = reduction (car, popped) = reduction (a,b) = c
P = {∅}
push(car, P) -> P = {c}
i = 3 :
car = S[i] = a, P = {c}
P is not empty :
head = c
head != car ->
popped = Pop(P) = c
car = reduction (car, popped) = reduction (a,c) = b
P = {∅}
push(car, P) -> P = {b}
...
i = 5 : (interesting case)
car = S[i] = c, P = {c}
P is not empty :
head = c
head == car -> break
push(car, P) -> P = {c, c}
i = 6 :
car = S[i] = b, P = {c, c}
P is not empty :
head = c
head != car ->
popped = Pop(P) = c
car = reduction (car, popped) = reduction (b,c) = a
P = {c}
P is not empty : // (note in this case car = a)
head = c
head != car ->
popped = Pop(P) = c
car = reduction (car, popped) = reduction (a,c) = b
P = {∅}
push(car, P) -> P = {b}
... and it continues until n
我已经在这样的各种示例上运行了这个算法,它似乎有效。 我用 Java 编写了一个代码来测试这个算法,当我将代码提交给系统时,我得到了错误的答案。我已经在gisthub 上发布了java 代码,所以你可以看到它。
谁能告诉我我的算法出了什么问题。
【问题讨论】:
-
它要求最小的字符串,那么这意味着如果有不止一种方法可以减少字符串,你必须找到它。你好像只搜索第一种还原方式,当然会失败。有时,不使用规则并等待更多字符出现可能会产生更好的结果。
-
@acattle 是的,但仅在第一种情况下,第一个字符。在每个 for 循环中,堆栈至少有一个字符。
-
@nhahtdh 你能说得更具体点吗??
-
我认为不可能获得
O(n)算法... -
这个简单的算法可能有用jsfiddle.net/YFw4G JavaScript 很容易理解
标签: java algorithm pseudocode