[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:

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4.  
  5. char* LCS(char longer[], char shorter[])
  6. {
  7. int longer_len=strlen(longer);
  8. int shorter_len=strlen(shorter);
  9. int record[shorter_len];
  10. int i,j,max_len=,substr_end=,substr_begin;
  11.  
  12. for(i=;i<longer_len;i++)
  13. {
  14. for(j=shorter_len-;j>=;j--)
  15. {
  16. if(longer[i]==shorter[j])
  17. {
  18. if(==i || ==j)
  19. {
  20. record[j]=;
  21. }
  22. else
  23. {
  24. record[j]=record[j-]+;
  25. }
  26. }
  27. else
  28. {
  29. record[j]=;
  30. }
  31.  
  32. if(record[j]>max_len)
  33. {
  34. max_len=record[j];
  35. substr_end=j;
  36. }
  37. }
  38. }
  39.  
  40. substr_begin=substr_end-max_len+;
  41.  
  42. // char substr[max_len];
  43. char* substr=(char*)malloc(max_len);
  44. for(i=;i<max_len;i++)
  45. {
  46. substr[i]=shorter[substr_begin+i];
  47. }
  48. return substr;
  49. }
  50.  
  51. int main()
  52. {
  53. char longer[]="asdfghjklqwertyyuio";
  54. char shorter[]="zxvsdffghwerxv";
  55.  
  56. printf("The longer str:\n\t%s\n",longer);
  57. printf("The shorter str:\n\t%s\n",shorter);
  58. printf("The lagest common str:\n\t%s\n",LCS(longer,shorter));
  59. return ;
  60. }

[006] largest_common_substring的更多相关文章

  1. 《zw版·Halcon-delphi系列原创教程》 Halcon分类函数006, image,影像处理(像素图)

    <zw版·Halcon-delphi系列原创教程> Halcon分类函数006, image,影像处理(像素图) 为方便阅读,在不影响说明的前提下,笔者对函数进行了简化: :: 用符号“* ...

  2. android 入门 006(sqlite增删改查)

    android 入门 006(sqlite增删改查) package cn.rfvip.feb_14_2_sqlite; import android.content.Context; import ...

  3. php大力力 [006节]初步接触认识phpMyAdmin

    phpMyAdmin 2015-08-22 php大力力006. 初步接触认识phpMyAdmin 以下是phpAdmin网络截图: 这是通过MAMP一键安装的. php中MyAdmin的使用-猿代码 ...

  4. [反汇编练习] 160个CrackMe之006

    [反汇编练习] 160个CrackMe之006. 本系列文章的目的是从一个没有任何经验的新手的角度(其实就是我自己),一步步尝试将160个CrackMe全部破解,如果可以,通过任何方式写出一个类似于注 ...

  5. 2017-2018-1 1623 bug终结者 冲刺006

    bug终结者 冲刺006 by 20162328 蔡文琛 今日任务:音频素材添加 又是新的一天,小组项目有了很大的起色,已经可以在手机上试玩了. 添加背景音乐能使我们的游戏锦上添花. 音频资源需求 需 ...

  6. Python:每日一题006

    题目:斐波那契数列. 程序分析:斐波那契数列(Fibonacci sequence),又称黄金分割数列,指的是这样一个数列:0.1.1.2.3.5.8.13.21.34.…… 个人思路及代码: # 方 ...

  7. AtCoder Grand Contest 006

    AtCoder Grand Contest 006 吐槽 这套题要改个名字,叫神仙结论题大赛 A - Prefix and Suffix 翻译 给定两个串,求满足前缀是\(S\),后缀是\(T\),并 ...

  8. 6.006 Introduction to Algorithms

    课程信息 6.006 Introduction to Algorithms

  9. 『006』Shell脚本

    『003』索引-Linux Shell Script Shel脚本-初步入门 [001]- 点我快速打开文章[<01 什么是 Shell>] [002]- 点我快速打开文章[<02 ...

随机推荐

  1. LoadRunner脚本增强技巧之参数化(一)

    参数化的方式有两种,一种通过File引入参数值,一种通过数据库引入参数值.本篇介绍File方式引入参数值. 一.File方式参数化过程 1.在脚本中找到需要做参数化的字符串,选中,右键点击,选择Rep ...

  2. CF464C-Substitutes in Number

    题意 开始给出一个长为\(n\)的数字串,有\(m\)次操作按顺序执行,每次把当前数字串中的某一个数码替换成一个数字串\(t\)(可以为空或多位),最后问操作结束后的数字串十进制下模\(10^9+7\ ...

  3. POJ 3276 Face The Right Way(前缀和优化)

    题意:有长度为N的01串,有一个操作可以选择连续K个数字取反,求最小的操作数和最小的K使得最后变成全1串.(N<=5000) 由于K是不定的,无法高斯消元. 考虑枚举K,求出最小的操作数. 显然 ...

  4. BZOJ3560 DZY Loves Math V(欧拉函数)

    对每个质因子分开计算再乘起来.使用类似生成函数的做法就很容易统计了. #include<iostream> #include<cstdio> #include<cmath ...

  5. 题解 P1469 【找筷子】

    这题真是水 咳咳.. 基本思路:桶排 但是可以剪枝. 剪枝方法: 好几种,可以用set(集合),可以用stack(栈), 也可以像我一样的蒟蒻最大最小值...... 但是作者的毒瘤数据应该不会放过我们 ...

  6. 【BZOJ3771】Triple(生成函数,多项式运算)

    [BZOJ3771]Triple(生成函数,多项式运算) 题面 有\(n\)个价值\(w\)不同的物品 可以任意选择\(1,2,3\)个组合在一起 输出能够组成的所有价值以及方案数. \(n,w< ...

  7. 洛谷P1602 Sramoc问题 题解报告【同余+bfs】

    题目描述 话说员工们整理好了筷子之后,就准备将快餐送出了,但是一看订单,都傻眼了:订单上没有留电话号码,只写了一个sramoc(k,m)函数,这什么东西?什么意思?于是餐厅找来了资深顾问团的成员,YQ ...

  8. Hexo之我的桌角女友的食用方式

    秀秀 通过使用一个名为 hexo-helper-live2d 的开源库,可以轻松的在自己的Hexo网站下贴上一只生猛可爱的萌妹子或主子: 什么是live2d Live2d是11区宅男们开发出的虚拟女友 ...

  9. python安装包下载

    加入python官网一次按照下图点击: 这个exe文件就下好了,然后再安装一下即可.

  10. [DeeplearningAI笔记]卷积神经网络4.6-4.10神经网络风格迁移

    4.4特殊应用:人脸识别和神经网络风格转换 觉得有用的话,欢迎一起讨论相互学习~Follow Me 4.6什么是神经网络风格转换neural style transfer 将原图片作为内容图片Cont ...