[006] largest_common_substring
[Description] Given two different strings, find the largest successive common substring.
e.g. str1[]="qwertyuiopasdfgh"; str2[]="jhdfgqwertyudfxcv";
LCsubstr[]="qwertyu";
[Thought] show the correspondence result of this two strings from the 2-D array of 'record[][]', and find the longest non-zero diagonal elements. To save the auxiliary space, we can use only 1-D array 'record[]' to implement. O(n^2) O(m)
[Implementation] C code:
#include<stdio.h>
#include<stdlib.h>
#include<string.h> char* LCS(char longer[], char shorter[])
{
int longer_len=strlen(longer);
int shorter_len=strlen(shorter);
int record[shorter_len];
int i,j,max_len=,substr_end=,substr_begin; for(i=;i<longer_len;i++)
{
for(j=shorter_len-;j>=;j--)
{
if(longer[i]==shorter[j])
{
if(==i || ==j)
{
record[j]=;
}
else
{
record[j]=record[j-]+;
}
}
else
{
record[j]=;
} if(record[j]>max_len)
{
max_len=record[j];
substr_end=j;
}
}
} substr_begin=substr_end-max_len+; // char substr[max_len];
char* substr=(char*)malloc(max_len);
for(i=;i<max_len;i++)
{
substr[i]=shorter[substr_begin+i];
}
return substr;
} int main()
{
char longer[]="asdfghjklqwertyyuio";
char shorter[]="zxvsdffghwerxv"; printf("The longer str:\n\t%s\n",longer);
printf("The shorter str:\n\t%s\n",shorter);
printf("The lagest common str:\n\t%s\n",LCS(longer,shorter));
return ;
}
[006] largest_common_substring的更多相关文章
- 《zw版·Halcon-delphi系列原创教程》 Halcon分类函数006, image,影像处理(像素图)
<zw版·Halcon-delphi系列原创教程> Halcon分类函数006, image,影像处理(像素图) 为方便阅读,在不影响说明的前提下,笔者对函数进行了简化: :: 用符号“* ...
- android 入门 006(sqlite增删改查)
android 入门 006(sqlite增删改查) package cn.rfvip.feb_14_2_sqlite; import android.content.Context; import ...
- php大力力 [006节]初步接触认识phpMyAdmin
phpMyAdmin 2015-08-22 php大力力006. 初步接触认识phpMyAdmin 以下是phpAdmin网络截图: 这是通过MAMP一键安装的. php中MyAdmin的使用-猿代码 ...
- [反汇编练习] 160个CrackMe之006
[反汇编练习] 160个CrackMe之006. 本系列文章的目的是从一个没有任何经验的新手的角度(其实就是我自己),一步步尝试将160个CrackMe全部破解,如果可以,通过任何方式写出一个类似于注 ...
- 2017-2018-1 1623 bug终结者 冲刺006
bug终结者 冲刺006 by 20162328 蔡文琛 今日任务:音频素材添加 又是新的一天,小组项目有了很大的起色,已经可以在手机上试玩了. 添加背景音乐能使我们的游戏锦上添花. 音频资源需求 需 ...
- Python:每日一题006
题目:斐波那契数列. 程序分析:斐波那契数列(Fibonacci sequence),又称黄金分割数列,指的是这样一个数列:0.1.1.2.3.5.8.13.21.34.…… 个人思路及代码: # 方 ...
- AtCoder Grand Contest 006
AtCoder Grand Contest 006 吐槽 这套题要改个名字,叫神仙结论题大赛 A - Prefix and Suffix 翻译 给定两个串,求满足前缀是\(S\),后缀是\(T\),并 ...
- 6.006 Introduction to Algorithms
课程信息 6.006 Introduction to Algorithms
- 『006』Shell脚本
『003』索引-Linux Shell Script Shel脚本-初步入门 [001]- 点我快速打开文章[<01 什么是 Shell>] [002]- 点我快速打开文章[<02 ...
随机推荐
- 第88天:HTML5中使用classList操作css类
在HTML5 API里,页面DOM里的每个节点上都有一个classList对象,程序员可以使用里面的方法新增.删除.修改节点上的CSS类.使用classList,程序员还可以用它来判断某个节点是否被赋 ...
- 后缀树的线性在线构建-Ukkonen算法
Ukkonen算法是一个非常直观的算法,其思想精妙之处在于不断加字符的过程中,用字符串上的一段区间来表示一条边,并且自动扩展,在需要的时候把边分裂.使用这个算法的好处在于它非常好写,代码很短,并且它是 ...
- XTU 1233 Coins(DP)
题意: n个硬币摆成一排,问有连续m个正面朝上的硬币的序列种数. 很明显的DP题.定义状态dp[i][1]表示前i个硬币满足条件的序列种数.dp[i][0]表示前i个硬币不满足条件的序列种数. 那么显 ...
- "strcmp()" Anyone? UVA - 11732(trie出现的次数)
给你n个单词,让他们两两比较,要求他们运用strcmp时,进行比较的次数. 边建树边统计 #include <iostream> #include <cstdio> #incl ...
- JS中数组和字符串具有的方法,以及substring,substr和slice的用法与区别
String 对象属性 属性 描述 constructor 对创建该对象的函数的引用 length 字符串的长度 prototype 允许您向对象添加属性和方法 String 对象方法 方法 描述 ...
- BIOS和CMOS的区别
原文链接:https://www.cnblogs.com/boltkiller/articles/5732424.html 在日常操作和维护计算机的过程中,常常可以听到有关BIOS设置和CMOS设置的 ...
- 第一个python程序(2)
输入和输出 输出 用print加上字符串,就可以向屏幕上输出指定的文字.比如输出'hello, world',用代码实现如下: >>> print 'hello, world' pr ...
- NOIP2017 Day1 T3 逛公园(最短路+拓扑排序+DP)
神tm比赛时多清个零就有60了T T 首先跑出1起点和n起点的最短路,因为k只有50,所以可以DP.设f[i][j]表示比最短路多走i的长度,到j的方案数. 我们发现如果在最短路上的和零边会有后向性, ...
- 框架----Django之ModelForm组件
ModelForm a. class Meta: model, # 对应Model的 fields=None, # 字段 exclude=None, # 排除字段 labels=None, # 提示信 ...
- Educational Codeforces Round 20 C 数学/贪心/构造
C. Maximal GCD time limit per test 1 second memory limit per test 256 megabytes input standard input ...