#-*-coding:utf-8-*- ######################################################################### # Copyright (C) 2017 All rights reserved. # # FileName:GetLongestSubString.py # Creator: x‘x‘[email protected] # 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]\n"); 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!\n"%str(sys.argv[0])); sys.stderr.write("time elapse:%f\n"%interval);
时间: 2024-10-13 16:58:10