2013年9月16日星期一

How to calculate the two middle strings continuous string

Conditions: 1 , two strings "AAA00001" , and "AAA0000200000", known to the same part of the "AAA0000".
2, using java , how to calculate the continuum between these two strings strings , namely the so-called continuous as follows:

AAA00001, AAA00002, AAA00003 ... AAA000010, AAA000011 ... AAA0000200000

continuous string can be seen from the above did not change the part of the "AAA0000", becoming only the second half .



Please respect Tell me what you pass , cattle, help ... Great God to spare the next request advice , request to throw a little routine reference information.

My point really is not much, 20 points for help, first thanked .
------ Solution ---------------------------------------- ----
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Demo {
public static void main(String[] args) {
String first = "Cc001";
String last = "Cc00200";
betweenStr(first, last);
}

public static void betweenStr(String first, String last) {
String prefix = null;
Matcher m = Pattern.compile("(.*)([^0])").matcher(last);
if (m.find()) {
prefix = m.group(1);
}
int begin = Integer.parseInt(first.replace(prefix, ""));
int end = Integer.parseInt(last.replace(prefix, ""));
for (int i = begin + 1; i < end; i++) {
System.out.println(prefix + i);
}
}

}

------ For reference only ---------------------------- -----------
wait for the answer - wait for the answer - wait for the answer - wait for the answer - wait for the answer UP
------ For reference only ------- --------------------------------
public static void main(String[] args) {
System.out.println(find("1aaaa1234", "aaaa123"));
}

public static String find(String args1, String args2) {
String temp = "";
String maxstr = "";
for (int i = 0; i < args1.length(); i++) {
for (int j = 0; j < args2.length(); j++) {
for (int k = 1; (k + i) <= args1.length()
&& (k + j) <= args2.length(); k++) {
if (args1.substring(i, k + i).equals(
args2.substring(j, k + j))) {
temp = args1.substring(i, k + i);
} else {
if (temp.length() > maxstr.length())
maxstr = temp;
temp = "";
}
}
if (temp.length() > maxstr.length())
maxstr = temp;
temp = "";
}
}
return maxstr;
}

------ For reference only ---------------------------------------
this man so kind Reply me soon ths
------ For reference only ---------------------------- -----------
not it does not match the meaning of the questions ah boss
------ For reference only ---------------------------------------
to get AAA0001 .... AAA000n
still have to get a number between AAA0001 and AAAA000N ?
------ For reference only -------------------------------------- -
to get AAA0001 .... AAA000n
form as follows:
AAA00001, AAA00002, AAA00003 ... AAA000010, AAA000011 ... AAA0000200000
------ For reference only --- ------------------------------------
that Italy does not match the second floor ? I did not run , can not ah , I feel very good , lz do not know what they want
------ For reference I want only ---------------------------------------
faint is such
give you two strings "AAA00001" and "AAA0000200000", known to the same part of the "AAA0000"
print out a string between two strings can all consecutive , print the following form:

AAA00001, AAA00002, AAA00003 ... AAA000010, AAA000011 ... AAA0000200000
------ For reference only ------------- --------------------------
public class Demo {
public static void main(String[] args) {
String first = "AAA00001";
String last = "AAA0000200";
betweenStr(first, last);
}

public static void betweenStr(String first, String last) {
int begin = Integer.parseInt(first.replace("AAA0000", ""));
int end = Integer.parseInt(last.replace("AAA0000", ""));
for (int i = begin + 1; i < end; i++) {
System.out.println("AAA0000" + i);
}
}

}

------ For reference only ------ ---------------------------------
public class StringUtil {

public static void main (String [] args) {
String str1 = "abc000010";
String str2 = "abc000030000";
System.out.println (computeTickets (str1, str2));

}

/ **
* get the prefix
* @ param str
* @ return
* /
private static String getPrefix (String str) {
return str.replaceAll ("[^ a-zA-Z]. + $", "");
}

/ **
* get suffix
* @ param str
* @ return
* /
private static long getSuffix (String str) {
Matcher ma = Pattern.compile ("\ \ d +."). matcher (str);
while (ma.find ()) {
return Long.parseLong (ma.group ());
}
return 0;
}

/ **
* compute the connection between the two votes vote No.
* @ param str1
* @ param str2
* @ return
* /
public static List computeTickets (String str1, String str2) {
if (getSuffix (str1) == 0 | | getSuffix (str2) == 0) {
return new ArrayList ();
}
int index = str2.length () - getPrefix (str2). length ();
List strList = new ArrayList ();
for (long i = getSuffix (str1); i <= getSuffix (str2); i + +) {
strList.add (getPrefix (str1) + String.format ("% 0" + index + "d", i));
}
return strList;
}
}

These are my own writing, but print out or can not meet format requirements , and to my desired format is like this
abc000010, abc000011, abc000012 .... abc000030000 but I did not do it so can not help
------ For reference only ---------------------------------------
9 floor method really can you get me a few lines So now you really did not engage in out -line ! ! !
------ For reference only -------------------------------------- -

---- - For reference only ---------------------------------------


you put the same parts replaced with " " , the premise is that you can now see AAA0000 this part is the same, if I re-transmission into two strings , such as:
CCC001 and CCC0020000, then this method is not generic , and therefore must be able to CCC00 extracted from this same part of the first job , but I did not realize that you have any good way to do it ?
------ For reference only -------------------------------------- -


V5 ah positive solution to the sub- sub- knot tie much huh
------ For reference only ------- --------------------------------

------ For reference only -------------- -------------------------
has given collection points , please
------ For reference only ---------------------------------------
import java.util.regex . Matcher;
import java.util.regex.Pattern;

public class Demo {
public static void main (String [] args) {
String first = "AAcc0001";
String last = "AAcc0006666";
betweenStr (first, last);
}

public static void betweenStr (String first, String last) {
String prefix = "";
Matcher m = Pattern.compile ("(. *) ([^ 0])"). matcher (last ) ;
if (m.find ()) {
prefix = m.group (1);
}
int begin = Integer.parseInt (first.replace (prefix, ""));
int end = Integer.parseInt (last.replace (prefix, ""));
for (int i = begin + 1; i System.out.println (prefix + i);
}
prefix = "";
}
}

Why do I pass two parameters to die above it ? ? Call 16 F
------ For reference only ----------------------------- ----------
yesterday saw Lz topic , quite interested , began to try to prepare .
wrote one day , seemingly feasible , Lz can try.
Although knot posted already , but still want to have the experts can give pointers pointing small rookie .

public class SameTest {
/**
 *
 * @author Milo 2013-9-10
 * @param args
 */
public static void main(String[] args) {
String str1 = "dddabcdef00000001aaaaa" ;
String str2 = "aabcdef00000010" ;
// String str1 = "AAA00001" ;
// String str2 = "AAA0000200000" ;
// String str1 = "AAcc0001" ;
// String str2 = "AAcc0006666" ;
String sameSubStr = find(str1 , str2) ; //共同部分

String subStr1 = str1.substring(str1.indexOf(sameSubStr)) ; //删除前段字母
String subStr2 = str2.substring(str2.indexOf(sameSubStr)) ; //删除前段字母
String resultStr1 = "" ;
String resultStr2 = "" ;
resultStr1 = resultStr(subStr1) ; //删除后段字母
resultStr2 = resultStr(subStr2) ; //删除后段字母
StringBuffer charSB = new StringBuffer() ; //保存共同部分中字母以及开头的0,例如:abcdef0000000
StringBuffer intSB1 = new StringBuffer() ; //保存共同部分中数字部分
for(int x = 0 ; x < resultStr1.length() ; x++){
if(isInteger(String.valueOf(resultStr1.charAt(x)))){
if(resultStr1.charAt(x) == '0'){
charSB.append(resultStr1.charAt(x)) ;
}
else{
intSB1.append(resultStr1.substring(x)) ;
break ;
}
}
else{
charSB.append(resultStr1.charAt(x)) ;
}
}

StringBuffer intSB2 = new StringBuffer() ;
for(int x = 0 ; x < resultStr2.length() ; x++){
if(isInteger(String.valueOf(resultStr2.charAt(x)))){
if(resultStr2.charAt(x) == '0'){
}
else{
intSB2.append(resultStr2.substring(x)) ;
break ;
}
}
}
Integer in1 = Integer.parseInt(intSB1.toString()) ;
Integer in2 = Integer.parseInt(intSB2.toString()) ;
while(in1 <= in2){
String result = charSB.toString() ;
result = result + in1.toString() ;
System.out.println("Results are :" + result);
in1++ ;
}
}
//对比两个字符串,返回最长的相同部分
public static String find(String s1 , String s2){
Integer i = 0 ;
String sameStr = "" ;
StringBuffer sb = new StringBuffer() ;
for(int x = 0 ; x < s2.length() ; x++){
if(i<= s1.length()){
sb.append(s2.charAt(x)) ;
if(s1.contains(sb.toString())){
if(sameStr.length() <= sb.toString().length()){
sameStr = sb.toString() ;
}
}
else{
sb.replace(0 , x + 1, "") ;
i++ ;
x = i - 1 ;
}
}
}
return sameStr ;
}
//判断结尾是否为数字
public static boolean isInteger(String value) {
try {
Integer.parseInt(value);
return true;
}catch (NumberFormatException e) {
return false;
}
}
//删除后段字母,留中段数字
public static String resultStr(String s){
String resultStr = "" ;
for(int x = s.length() - 1; x >=0 ; x--){
if(!isInteger(String.valueOf(s.charAt(x)))){
resultStr = s.substring(0, x) ;
}
else{
resultStr = s.substring(0, x + 1) ;
break ;
}
}
return resultStr ;
}
}

------ For reference only ----------------------------------- ----
I waited a few days thought no one would come to help me answer this question , 19th Floor, Oh thank Ha I'm sorry brother has a tie knot ; can not give divided

没有评论:

发表评论