zoukankan      html  css  js  c++  java
  • newwork setup

    #-*-coding:utf-8-*-
    #########################################################################
    #   Copyright (C) 2017 All rights reserved.
    # 
    #   FileName:GetLongestSubString.py
    #   Creator: x'x'x@xxx.com
    #   Time:06/02/2017
    #   Description:
    #
    #   Updates:
    #
    #########################################################################
    #!/usr/bin/python
    # please add your code here!
    import re;
    import sys;
    import time;
    def PrintUsage():
        sys.stderr.write("program [IN]
    ");
        sys.exit(1);
    class Solution:
        def GetLongestSubString(self,s1,s2):
            '''
             compute longest substring of input string s1 and s2
            '''
            maxLen = 0;
            endIndex = 0;
            print("s1="+s1);
            print("s2="+s2);
            if ( len(s1) == 0 or len(s2) == 0 ):
                return "";
            table=[[] for i in range(len(s1))];
            for i in range(len(table)):
                table[i]=[0 for j in range(len(s2))];
            for i in range(0,len(s1)):
                for j in range(0,len(s2)):
                    if ( i == 0 or j == 0 ):
                        if ( s1[i]==s2[j] ):
                            table[i][j] = 1;
                            if ( table[i][j] > maxLen ):
                                maxLen = table[i][j];
                                endIndex = i;
                    else:
                        if ( s1[i]==s2[j] ):
                            table[i][j] = table[i-1][j-1]+1;
                            if (table[i][j] > maxLen):
                                maxLen = table[i][j];
                                endIndex = i;
            print(table);
            return s1[endIndex-maxLen+1:endIndex+1];
    if (__name__=="__main__"):
        if (len(sys.argv) != 1):
            PrintUsage();
            sys.exit(1);
        starttime = time.clock();
        s1="gcdef";
        s2="abcdef";
        sol = Solution();
        print(sol.GetLongestSubString(s1,s2));
        endtime = time.clock();
        interval = endtime - starttime;
        sys.stderr.write("%s has finished congratulations!
    "%str(sys.argv[0]));
        sys.stderr.write("time elapse:%f
    "%interval);
  • 相关阅读:
    c8051f交叉开关
    8052定时器2的用法
    poj1010
    poj2101
    poj1958
    poj3444
    poj2977
    DataTable 相关操作
    C#中string和StringBuilder的区别
    DataTable排序,检索,合并,筛选
  • 原文地址:https://www.cnblogs.com/finallyliuyu/p/6931912.html
Copyright © 2011-2022 走看看