找出两个字符串的最大公共子串,比如说s1="asdeferdere",s2="cvvsdefrrt"两者的最大公共子串为"sdef"
解决方案 »
- 利用udp传输过来的数据,更新JTree的节点信息,更新JTree遇到问题
- 求助:JNI中中文的问题该如何解决啊??(急)
- 【100分】求简单的记事本程序
- 如何在frame(上面有些jbutton)上用自己的图片作为背景?
- 用java 实现 读取windows 2003系统参数的问题
- 怪事情,重装系统后最简单的HelloWorld程序都不能编译成功?
- 了解websphere4.0的朋友近来看看!!谢谢!
- 高分在线,谁能帮我解决,我把所有的分都奉献出去!!!java 转换中的中文问题
- jublider5中的import问题????
- 怎么把主方法里面的代码移到外面来
- 转换为String的String[]数据如何再恢复成String[] ??
- 用XML 生成的动态树 但是不知道怎么加链接?
public static void main(String args[]){
String str1 = "asdeferdere";
String str2 = "cvvsdefrrt";
String temp = str1.length()>str2.length()?str2:str1;
String temp2 = temp==str1?str2:str1;
int maxCount = 0;
String maxSequence = "";
for(int i=0;i<temp.length();i++){
for(int j=i+1;j<temp.length();j++){
if(temp2.contains(temp.substring(i, j))){
int tempCount = j - i;
if(tempCount>maxCount){
maxCount = tempCount;
maxSequence = temp.substring(i, j);
}
}
}
}
System.out.println(maxSequence);
}
import java.util.List;public class Clothes {
public static void main(String[] args) {
String s2 = "45aefg1234";
String s1 = "12345aesadaefgfewfw";
zdggzc(s2, s1);
} static void zdggzc(String s1, String s2) {
String temp;
if (s1.length() < s2.length()) {
temp = s1;
s1 = s2;
s2 = temp;
temp = null;
}
List<String> zdggzc = new LinkedList<String>();
int result = 0;
StringBuilder maxSubStr = new StringBuilder(); for (int i = 0; i <= s1.length() - 1; i++) {
char curChar = s1.charAt(i);
int indexInS2 = 0;
boolean flag = false;
while (true) {
if (flag) {
break;
}
if (indexInS2 > s2.length() - 1) {
break;
}
indexInS2 = s2.indexOf(curChar, indexInS2);
if (indexInS2 == -1) {
break;
}
maxSubStr.append(curChar);
int offInS1 = i;
int offInS2 = indexInS2;
while (true) {
offInS1++;
offInS2++;
if (offInS1 > s1.length() - 1 || offInS2 > s2.length() - 1) {
flag = true;
break;
}
if (s1.charAt(offInS1) == s2.charAt(offInS2)) {
maxSubStr.append(s1.charAt(offInS1));
} else {
indexInS2++;
break;
}
} if (maxSubStr.length() >= result) {
result = maxSubStr.length();
zdggzc.add(maxSubStr.toString());
}
maxSubStr.delete(0, maxSubStr.length());
}
}
for (String string : zdggzc) {
System.out.println(string);
}
}
}
import java.util.Scanner;public class For_Test {
public static void main(String args[]){
Scanner scan=new Scanner(System.in);
System.out.println("Input your string1:");
String str1 = scan.next();
System.out.println("Input your string2:");
String str2 = scan.next();
String temp = str1.length()>str2.length()?str2:str1;
String temp2 = temp==str1?str2:str1;
int maxCount = 0;
String maxSequence = "";
for(int i=0;i<=temp.length()-1;i++){
for(int j=i;j<=temp.length()-1;j++){
if(temp2.contains(temp.substring(i, j+1))){
int tempCount = j - i+1;
if(tempCount>maxCount){
maxCount = tempCount;
maxSequence = temp.substring(i, j+1);
}
}
}
}
System.out.println("The max Sequence is:"+maxSequence);
}
}
public class String3 {
public static void main(String[] args) {
// TODO Auto-generated method stub String s1="aefrereff";
String s2="vbfereffmm";
int m=s1.length();
int n=s2.length();
String s4=null;
for(int i=0;i<m-1;i++)
{
for(int j=0;j<m-i;j++)
{
String s3=s1.substring(j,j+i+1);
if(s2.indexOf(s3)!=(-1))
{
s4=s3;
}
}
}
System.out.println(s4);
}}
public class Test {
public static void main(String[] args) {
String s1 = "asdeferdere",s2="cvvsdefrrt";
String smallStr,largeStr;
if(s1.length() > s2.length()){
smallStr = s2;
largeStr = s1;
}else {
smallStr = s1;
largeStr = s2;
}
String str;
int max = 0,count = 0;
String resultStr = null;
for(int i = 0;i < smallStr.length();i++){
str = String.valueOf(smallStr.charAt(i));
count = 0;
for(int j = i + 1;j < smallStr.length();j++){
if(largeStr.contains(str)){
count++;
if(count > max){
max = count;
resultStr = str;
}
str += String.valueOf(smallStr.charAt(j));
}
else {
break;
}
}
}
System.out.println("same str : " + resultStr + " size : " + resultStr.length());
}
}
当字串大小等于m时,用
用两个set把 等于m的字串都放进去出来,然后比较,用系统的方法,这样可以少很少重复操作,当没找到、
m-1重复上面操作。直到找到位置return;
import java.util.List;public class Test {
public static void main(String[] args){
String s1="asdeferdefr";
String s2="cvvsdefrrt";
System.out.println(method(s1,s2));
}
/**
* 求s1和s2的最大公共字符串
* @param s1
* @param s2
* @return
*/
public static List method(String s1,String s2){
List list =null;
boolean isHasStr=false;
for(int i=1;i<=s1.length();i++){
isHasStr=false;
for(int j=0;j<=s1.length()-i;j++){
String temp=s1.substring(j, j+i);
if(isHasStr(s2,temp)){
if(!isHasStr){
isHasStr=true;
list=new ArrayList();
}
list.add(temp);
}
}
}
return list;
}
/**
* 第一个字符串中是否含有第二个字符串,有返回true,否则返回false
* @param str 第一个字符串
* @param sub 第二个字符串
* @return 第一个字符串中含有第二个字符串返回true,否则返回false
*/
public static boolean isHasStr(String str,String sub){
if(str.indexOf(sub)>=0){
return true;
}
return false;
}}
String str1 = "asdeferdere";
String str2 = "cvvsdefrrt";
int m = str1.length();
int n = str2.length();
int maxLen = 0;
int sIndex = -1; for(int i=0; (i+maxLen)<m ; i++){
int tmp = 0;
for(int j=0; j<n; j++){
if((i+tmp)<m && str1.charAt(i+tmp) == str2.charAt(j))
tmp++;
else{
if(tmp > maxLen){
maxLen = tmp;
sIndex = i;
}
tmp = 0;
}
}
}
System.out.println(str1.substring(sIndex, maxLen+sIndex));
}