求最大回文子串(马拉车算法)
来源:互联网 发布:网络推手兼职平台 编辑:程序博客网 时间:2024/05/22 01:34
这是正常的马拉车算法
import java.util.Scanner;public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); while (in.hasNextLine()) { String s = in.nextLine(); System.out.println(manacher(s)); } } public static int manacher(String s) { int count =0;//记录最大回文 StringBuffer sb = new StringBuffer(); char[] c =s.toCharArray(); sb.append("#"); //对字符串进行封装 for (int i = 0; i < c.length; i++) { sb.append(c[i]); sb.append("#"); } int[] rad = new int[sb.length()];//记录新字符串以每个字符为中心的最大回文半径 char[] cl = sb.toString().toCharArray(); int max=0;//记录已经搜寻到的回文半径能到达右端的最达大值 int id=0;//记录回文半径能到达最有端的回文字符串的中心 for (int i = 1; i < cl.length; i++) { if (max>i) { rad[i]=Math.min(rad[2*id-i], max-i); }else { rad[i]=1; } while (i-rad[i]>=0 && i+rad[i]<cl.length && cl[(i-rad[i])]==cl[(i+rad[i])]) { rad[i]++; } if (i+rad[i]>max) { max=i+rad[i]; id=i; } count=Math.max(count, rad[i]-1); } return count; }}
下面这个是我写的算法,运行起来没有上面的效率好,但是重在好理解
import java.util.Scanner;public class 回文字符串 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while(sc.hasNext()){ String str1 = sc.nextLine(); MaxHuiwen(str1); } } public static void MaxHuiwen(String s){ int count = 0; StringBuffer sb = new StringBuffer(); char[] c = s.toCharArray(); sb.append("#"); for (int i = 0; i < c.length; i++) { sb.append(c[i]); sb.append("#"); } int rad[] = new int[sb.length()]; char cl[] = sb.toString().toCharArray(); int max = 0; int id = 0; for (int i = 1; i < cl.length; i++) { rad[i] = 1; while(i-rad[i]>=0 && i+rad[i]<cl.length && cl[(i-rad[i])]==cl[(i+rad[i])]) { rad[i] ++; } if(i + rad[i] > max){ max = i + rad[i]; id = i; } count = Math.max(count, max); } System.out.println(count); } public static void MaxHuiwen(String str1, String str2){ int alen = str1.length() + 1; int blen = str2.length() + 1; int c[][] = new int[alen][blen]; int max = 0; for (int i = 1; i < alen; i++) { for (int j = 1 ; j < blen; j++) { if(str1.charAt(i - 1) == str2.charAt(j - 1)) c[i][j] = c[i - 1][j - 1] + 1; else c[i][j] = 0; if(c[i][j] > max) max = c[i][j]; } } System.out.println(max); }}
阅读全文
0 0
- 求最大回文子串(马拉车算法)
- Manacher马拉车算法求最长回文子串
- 最长回文子串-----“马拉车”算法
- 最长回文子串(马拉车)
- HDU 3068 manachar(马拉车)算法 求最长回文子串
- 求字符串的最长回文子串--最直观的“马拉车算法”分析
- mannachar(马拉车)求最长回文子串
- hihoCoder 最长回文子串(manacher算法:马拉车)
- Manacher算法--O(n)回文子串算法(马拉车算法)
- 【LeetCode】Longest Palindromic Substring(最长回文子串1)[马拉车Manacher算法]
- hihoCoder-1032 - 最长回文子串(Manacher 马拉车)
- 求最大回文子串(Manacher算法)
- 最长回文字符串(马拉车算法)
- 1040. Longest Symmetric String (25)【最长回文子串-马拉车(manacher算法)——PAT (Advanced Level) Practise
- 最长回文 --马拉车算法
- 求最大回文子串
- 线性时间求最大回文子串的Manacher算法
- HDU3948:后缀数组+马拉车(本质不同回文子串统计)
- Java异常(Java核心技术卷一读书笔记)
- imageloder
- CentOS6.7-64bit编译hadoop2.6.4
- codevs 1183 泥泞的道路 最长路 解题报告
- Git使用
- 求最大回文子串(马拉车算法)
- tablelayout+dreamlayout+Xlistview+lunbo
- recycleview的头布局和尾部局
- divided-and-conquer-106. Construct Binary Tree from Inorder and Postorder Traversal
- 深入理解JVM之虚拟机性能监控与故障处理工具
- WebService CXF框架的使用(实现JSON与XML数据传输)
- CodeVS1183 泥泞的道路 解题报告【二分答案+SPFA最长路】
- AndroidManifest主题设置
- c++运算符重载、类型转换函数总结