找到两个字符串之间最长的公共子序列时出错

时间:2015-04-29 13:44:39

标签: c debugging string-matching lcs

from multiprocessing import Process, Pipe
from threading import Thread
import time
from PySide import QtGui

class MyProcess(Process):

    def __init__(self, child_conn):
        super().__init__()
        self.child_conn = child_conn

    def run(self):
        # start a qt application
        app = QtGui.QApplication([])
        window = QtGui.QWidget()
        layout = QtGui.QVBoxLayout(window)
        button = QtGui.QPushButton('Test')
        button.clicked.connect(self.print_something)
        layout.addWidget(button)
        window.show()

        # start thread which listens on the child_connection
        t = Thread(target=self.listen, args = (app,))
        t.start()

        app.exec_() # this will block this process until somebody calls app.quit

    def listen(self, app):
        while True:
            message = self.child_conn.recv()
            if message == 'stop now':
                app.quit()
                return

    def print_something(self):
        print("button pressed")

if __name__ == '__main__':
    parent_conn, child_conn = Pipe()
    s = MyProcess(child_conn)
    s.start()
    time.sleep(5)
    parent_conn.send('stop now')
    s.join()

函数print_lcs从m,n开始并用于打印子序列。 lcs函数将找到lcs两个d数组b有字符C,U,L表示左上,上,左元素。我得到的输出不是答案,它给出了比实际答案短的子序列。

输入seq 1 = #include <stdio.h> #include <string.h> int m,n,a,c[20][20]; char x[15],y[15],b[20][20]; void print_lcs(int i,int j) { if(i==0 || j==0) return; if(b[i][j]=='C') { print_lcs(i-1,j-1); printf("%c",x[i-1]); } else if(b[i][j]=='U') print_lcs(i-1,j); else print_lcs(i,j-1); } void lcs() { int i,j; m=strlen(x); n=strlen(y); for(i=0;i<=m;i++) c[i][0]=0; for(i=0;i<=n;i++) { printf("0\t"); c[0][i]=0; } printf("\n"); for(i=1;i<=m;i++) { printf("0\t"); for(j=1;j<=n;j++) { if(x[i-1]==y[i-1]) { c[i][j]=c[i-1][j-1]+1; b[i][j]='C'; printf("%dC\t",c[i][j]); } else if(c[i-1][j]>=c[i][j-1]) { c[i][j]=c[i-1][j]; b[i][j]='U'; printf("%dU\t",c[i][j]); } else { c[i][j]=c[i][j-1]; b[i][j]='L'; printf("%dL\t",c[i][j]); } } printf("\n\n"); } printf("\nLongest common subsequence is:"); print_lcs(m,n); printf("\n"); printf("\nThe length of the subsequence is:%d",c[m][n]); } int main() { printf("Enter the 1st sequence:"); scanf("%s",&x); printf("\nEnter the 2nd sequence:"); scanf("%s",&y); lcs(); printf("\n"); getch(); return 0; } &amp; seq 2 = 1000101011我将lcs设为00011101,而实际答案为001

0 个答案:

没有答案