最长公共子序列与最长连续公共子序列-Java

柔情只为你懂 2021-11-23 02:50 432阅读 0赞

最长公共子序列与最长连续公共子序列(java)

1、最长公共子序列,顺序是一致并且相等,但是字符之间可以不是连续的。

2、求最长公共字符串,这就要求既是公共的字符串,又需要是连续的。

  1. package pratice725;
  2. //最长公共子序列与最长连续公共子序列(java)
  3. //顺序是一致并且相等,但是字符之间可以不是连续的。
  4. public class findLCS {
  5. public static void findLCS(String str,String patt){
  6. int strl = str.length();
  7. int pattl = patt.length();
  8. int lcs[][] = new int[strl][pattl];
  9. char strc[] = str.toCharArray();
  10. char pattc[] = patt.toCharArray();
  11. if(str==null || strl==0 || patt==null || pattl==0) return;
  12. int t = 0;
  13. for(int i =0;i<strl;i++){
  14. if(strc[i]==pattc[0])
  15. {
  16. t = 1;
  17. }
  18. lcs[i][0]=t;
  19. }
  20. t = 0;
  21. for(int j=0;j<pattl;j++){
  22. if(strc[0]==pattc[j]){
  23. t = 1;
  24. }
  25. lcs[0][j] = t;
  26. }
  27. for(int i=1;i<strl;i++){
  28. for(int j=1;j<pattl;j++)
  29. {
  30. if(strc[i]==pattc[j]){
  31. lcs[i][j] = lcs[i-1][j-1]+1;
  32. }else{
  33. lcs[i][j] = Math.max(lcs[i-1][j], lcs[i][j-1]);
  34. }
  35. }
  36. }
  37. System.out.println(“最大公共子序列长度矩阵是: “);
  38. for(int i=0;i<strl;i++){
  39. for(int j=0;j<pattl;j++)
  40. {
  41. System.out.print(lcs[i][j]+” “);
  42. }
  43. System.out.println(“”);
  44. }
  45. System.out.println(“最大公共子序列长度是: “+lcs[strl-1][pattl-1]);
  46. }
  47. //求最长公共字符串,这就要求既是公共的字符串,又需要是连续的。
  48. public static void getLCSString(String str,String patt){
  49. int strl = str.length();
  50. int pattl = patt.length();
  51. int lcs[][] = new int[strl][pattl];
  52. char strc[] = str.toCharArray();
  53. char pattc[] = patt.toCharArray();
  54. if(str==null || strl==0 || patt==null || pattl==0) return;
  55. int t = 0;
  56. for(int i =0;i<strl;i++){
  57. if(strc[i]==pattc[0])
  58. {
  59. t = 1;
  60. }else{
  61. t=0;
  62. }
  63. lcs[i][0]=t;
  64. }
  65. t = 0;
  66. for(int j=0;j<pattl;j++){
  67. if(strc[0]==pattc[j]){
  68. t = 1;
  69. }else{
  70. t=0;
  71. }
  72. lcs[0][j] = t;
  73. }
  74. for(int i=1;i<strl;i++){
  75. for(int j=1;j<pattl;j++)
  76. {
  77. if(strc[i]==pattc[j]){
  78. lcs[i][j] = lcs[i-1][j-1]+1;
  79. }else{
  80. lcs[i][j] = 0;
  81. }
  82. }
  83. }
  84. System.out.println(“最大公共子串矩阵是: “);
  85. for(int i=0;i<strl;i++){
  86. for(int j=0;j<pattl;j++)
  87. {
  88. System.out.print(lcs[i][j]+” “);
  89. }
  90. System.out.println(“”);
  91. }
  92. }
  93. public static void main(String[] args) {
  94. String str = “android”;
  95. String patt = “random”;
  96. findLCS(str,patt);
  97. System.out.println(“”);
  98. getLCSString(str,patt);
  99. }
  100. }

扫码关注一起随时随地学习!!!就在洋葱攻城狮,更多精彩,等你来!!

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3ljeTA3MDY_size_16_color_FFFFFF_t_70

发表评论

表情:
评论列表 (有 0 条评论,432人围观)

还没有评论,来说两句吧...

相关阅读

    相关 公共序列

    最长公共子序列 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 咱们就不拐弯抹角了,如题,需要你做的就是写一个程序,得出最长公共子序列

    相关 公共序列

    一个序列的子序列是该序列中删除某些元素后得到的序列。给定两个子序列X和Y,求二者的最长公共子序列。 例如,X=\{A,B,C,B,D,A,B\},Y=\{B,D,C,A,B,

    相关 公共序列

    最长公共子序列 最长公共子序列与最长公共子串的区别在于最长公共子序列不要求在原字符串中是连续的,比如ADE和ABCDE的最长公共子序列是ADE。 我们用动态规划的方法来思考