免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12
最近访问板块 发新帖
楼主: bleem1998
打印 上一主题 下一主题

Perl版有个有趣的题目:寻找最大公共串 [复制链接]

论坛徽章:
0
11 [报告]
发表于 2008-12-31 14:49 |只看该作者
写程序讲求实用,我这个速度还可以,而且也容易看懂。
你们搞那么多花里胡哨的东西,吓唬人还可以,呵呵。
#
a = '''Obama has not talked with the men in the past few weeks, friends said, and some of the president-elect's associates dismissed his connection to the trio as coincidence. But, if nothing else, the image of all three standing together in front of flashing cameras served as a reminder of the political environment in which Obama developed: Blagojevich is awaiting an indictment; Burris may be blocked from claiming the Senate seat by leaders in Illinois and Washington; and Rush pushed for a black senator to replace Obama, who prefers not to participate in "the politics of race."
'''
b = '''It wasn't the first time race had come up between Rush and Obama. Rush, a former Black Panther who represents Chicago's South Side, defeated Obama in 2000 because he dominated the black vote. During the campaign, he collected the endorsements of almost every major black politician in Chicago -- including Burris -- and depicted Obama as a foreigner to black culture. "Barack is a person who read about the civil rights protests and thinks he knows all about it," Rush said.
'''

dic = {}
for i in range(len(a)):
        for j in range(len(a),0,-1):
                if i<j:
                        dic[a[i:j]] = 0

dic2 = {}
for i in range(len(b)):
        for j in range(len(b),0,-1):
                if i<j:
                        dic2[b[i:j]] = 0

n = 0
s = 0
for i in dic.keys():
        if dic2.has_key(i) and len(i)>n:
                n = len(i)
                s = i[:]

print '"%s"'%s

[ 本帖最后由 julyzergcn 于 2008-12-31 16:02 编辑 ]

论坛徽章:
0
12 [报告]
发表于 2008-12-31 22:34 |只看该作者
简单实现一下lcs算法,不知道对不对。
str1 ='abcdef'
str2 ='qcfbcc'
sa=[]
line=[]
maxi=0
lcs={}
for i in range(0,len(str1)):
    line=[]
    for j in range(0,len(str2)):
        if str1[i:i+1] == str2[j:j+1]:
            if i==0 or j==0:
                line.append(1)
            else:
                line.append(sa[0][j-1]+1)
        else:
            line.append(0)
        if maxi<line[j]:
            lcs={}
            maxi=max(maxi,line[j])
            lcs=maxi
        elif maxi == line[j]:
            lcs=maxi
    sa.append(line)
   
    if len(sa)>1:
        sa.remove(sa[0])
   
for k,v in  lcs.iteritems():
    print str1[k-v+1:k+1]

[ 本帖最后由 luffy.deng 于 2009-1-2 08:59 编辑 ]

论坛徽章:
0
13 [报告]
发表于 2009-01-01 00:47 |只看该作者

回复 #11 julyzergcn 的帖子

知道这种程序的用途吗?在两个上百万个碱基对里面找出最长的匹配。你认为你的程序有实用价值吗?

论坛徽章:
0
14 [报告]
发表于 2009-01-01 09:06 |只看该作者

回复 #11 julyzergcn 的帖子

枚举子串不可取。还是生成匹配矩阵比较好,不知道有没有其他解法。

论坛徽章:
0
15 [报告]
发表于 2009-01-05 09:05 |只看该作者
这就是算法导论里的例题嘛

论坛徽章:
0
16 [报告]
发表于 2009-01-11 13:39 |只看该作者

回复 #2 xiaoyu9805119 的帖子

a='adddbcdfdghjdd'
b='abcdfghjddkdkdkkkk'
al=len(a)
bl=len(b)
def check(x,y):
for i in range(len(x),1,-1):
  for j in range(len(x)-i):
   if x[j:i+j] in y:
    return x[j:i+j]
if al<bl :
if a in b:
  length=a
else:
  length=check(a,b)
else:
if b in a :
  length=b
else:
  length=check(b,a)
print length
n=raw_input()
你看行不?

论坛徽章:
0
17 [报告]
发表于 2009-01-18 15:49 |只看该作者
动态规划O(N*M)... 不过脚本嘛就要体现出Py的特点
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP