[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 ...
随机推荐
- MVC 中创建自己的异常处理
1.新建类一个类继承 HandleErrorAttribute 类把异常书写到队列中 public class MyExceptionAttribute: HandleErrorAttribute { ...
- 第89天:HTML5中 访问历史、全屏和网页存储API
一.访问历史 API 通过history对象实现前进.后退和刷新之类的操作 history新增的两个方法history.replaceState()和history.pushState()方法属于HT ...
- js简易随机打乱数组方法
打乱随机数算法很多,不过看这个还挺简便的,记录下来. function shuffle(a) { var len = a.length; for(var i=0;i<len;i++){ var ...
- 51nod 1503 猪和回文(多线程DP)
虚拟两个点,一个从左上角开始走,一个从右下角开始走,定义dp[i][j][k]表示走了i步后,第一个点横向走了j步,第二个点横向走了k步后形成的回文方法种数. 转移方程显然可得,然后滚动数组搞一搞. ...
- luogu 1712 区间(线段树+尺取法)
题意:给出n个区间,求选择一些区间,使得一个点被覆盖的次数超过m次,最小的花费.花费指的是选择的区间中最大长度减去最小长度. 坐标值这么大,n比较小,显然需要离散化,需要一个技巧,把区间转化为半开半闭 ...
- BZOJ3717 PA2014Pakowanie(状压dp)
显然贪心地有尽量先往容量大的背包里放.设f[i]为i子集物品最小占用背包数,g[i]为该情况下最后一个背包的剩余容量,转移显然. #include<iostream> #include&l ...
- After ZJOI2017 day2
4.28早上6点左右就起了床,怀着紧张的心情,候到了7:45进考场 看到题,先0.5h看了看题意,yy一下,至少10+20+10. 首先是觉得T3可以搞一搞,先想到SA,很快就X掉了,思索一会儿,感觉 ...
- linux内核设计与实现第七周读书笔记
第七章 链接 链接(linking)是将各种代码和数据部分收集起来并组合成为一个单一文件的过程,这个文件可被加载(或被拷贝)到存储并执行.链接可以执行于编译时(compile time),也就是在源代 ...
- python 之 strip()--(转载)
原博地址:http://www.jb51.net/article/37287.htm 函数原型 声明:s为字符串,rm为要删除的字符序列 s.strip(rm) 删除s字符串中开头.结尾 ...
- 使用 ant 构建的一个例子
在项目根目录下新建一个 build.xml 文件,内容如下: <?xml version="1.0"?> <project name="javatest ...