50,528
社区成员
发帖
与我相关
我的任务
分享
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package test;
import java.util.ArrayList;
import java.util.List;
/**
*
* @author Administrator
*/
public class SubString {
/**
* @param args the command line arguments
*/
static boolean isIn(String sub, String In) {
return In.contains(sub);
}
static String[] getSubs(String In) {
char[] ins = In.toCharArray();
int inL = (int) Math.pow(2, In.length());
String[] asubs = new String[inL - 1];
String positions;
String aSub;
for (int i = 1; i < inL; i++) {
positions = Integer.toBinaryString(i);
if (positions.length() < ins.length) {
do {
positions = "0" + positions;
} while (positions.length() < ins.length);
}
char[] aCs = positions.toCharArray();
List<Character> aSubChars = new ArrayList<Character>();
for (int j = 0; j < aCs.length; j++) {
if (Character.digit(aCs[j], 10) == 1) {
aSubChars.add(ins[j]);
}
}
aSub = aSubChars.toString();
asubs[i - 1] = aSub;
}
return asubs;
}
public static void main(String[] args) {
String a = "dvsrwwwyui";
String b = "dvsrwwtyu";
String c = "dpseerwytiu";
String max = null;
int maxlength = 0;
String[] asubs = getSubs(a);
String[] bsubs = getSubs(b);
String[] csubs = getSubs(c);
for (int i = 0; i < asubs.length; i++) {
for (int j = 0; j < bsubs.length; j++) {
for (int k = 0; k < csubs.length; k++) {
if (asubs[i].equals(bsubs[j]) && asubs[i].equals(csubs[k])) {
if (csubs[k].length() > maxlength) {
maxlength = csubs[k].length();
max = csubs[k];
}
}
}
}
}
if (max != null) {
max = max.replace("[", "").replace("]", "").replace(",", "").replace(" ", "");
}
System.out.println(a);
System.out.println(b);
System.out.println(c);
System.out.println("结果:"+max);
}
}
dvsrwwwyui
dvsrwwtyu
dpseerwytiu
结果:dsrwyu