Day19 字符串匹配
作者:互联网
1.这个是朴素模式匹配算法,从主串头开始,依次选取和模式串等长的子串,挨个字符匹配,如果匹配失败就检索下一个子串。
2.匹配第一个字符的长度是 length - paraMyString.length + 1。比如主串是ABCDE,模式串是ACB,这里的第一个循环就应该比对 5 - 3 + 1 = 3次。如果第一个字符匹配就依次往后比对,所以第二个循环的比对次数就是模式串的长度 paraMyString.length 次。比如主串是ABCDCDE,模式串是CDE,这里第一个循环比对5次,第二个循环比对3次。
3.这里判断两字符相同 data[i + j] != paraMyString.data[j],这里在③就执行 tempMatch = false。如果没有匹配失败的就返回位置i了。
4.这里求子串for循环里的赋值用的是 resultMyString.data[i] = data[paraStartPosition + i],注意这里的下标; 5.转义符可以打印引号,还可以用转义符加数字打印ASCII码。
4.这里求子串for循环里的赋值用的是 resultMyString.data[i] = data[paraStartPosition + i],注意这里的下标; 5.转义符可以打印引号,还可以用转义符加数字打印ASCII码。
1 public class MyString { 2 /** 3 * The maximal length. 4 */ 5 public static final int MAX_LENGTH = 10; 6 7 /** 8 * The actual length. 9 */ 10 int length; 11 12 /** 13 * The data. 14 */ 15 char[] data; 16 17 /** 18 * Construct an empty char array. 19 */ 20 public MyString() { 21 length = 0; 22 data = new char[MAX_LENGTH]; 23 }// Of the first constructor 24 25 /** 26 * Construct using a system defined string. 27 * 28 * @param paraString The given sting. 29 * Its length should not exceed MAX_LENGtH - 1. 30 * 31 */ 32 public MyString(String paraString) { 33 data = new char[MAX_LENGTH]; 34 length = paraString.length(); 35 36 for (int i = 0; i < length; i++) { 37 data[i] = paraString.charAt(i); 38 } // Of for i 39 }// Of the second constructor 40 41 public String toString() { 42 String resultString = ""; 43 44 for (int i = 0; i < length; i++) { 45 resultString += data[i]; 46 } // Of for i 47 48 return resultString; 49 }// Of toSting 50 51 /** 52 * Locate the position of a substring. 53 * 54 * @param paraMyString The given substring. 55 * @return The first position.-1 for no matching. 56 */ 57 public int locate(MyString paraMyString) { 58 boolean tempMatch = false; 59 for (int i = 0; i < length - paraMyString.length + 1; i++) { 60 //Initialize. 61 tempMatch = true; 62 for (int j = 0; j < paraMyString.length; j++) { 63 if (data[i + j] != paraMyString.data[j]) { 64 tempMatch = false; 65 break; 66 } // Of if 67 } // Of for j 68 69 if (tempMatch) { 70 return i; 71 } // Of if 72 } // Of for i 73 return -1; 74 }// Of locate 75 76 77 /** 78 * Get a substring. 79 * @param paraString The given substring. 80 * @param paraStartPosition The start position in the original string. 81 * @param paraLength The length of the new sting. 82 * @return The first position.-1 for no matching. 83 */ 84 public MyString substring(int paraStartPosition, int paraLength) { 85 if (paraStartPosition + paraLength > length) { 86 System.out.println("The bound is exceeded"); 87 return null; 88 } // Of if 89 90 MyString resultMyString = new MyString(); 91 resultMyString.length = paraLength; 92 93 for (int i = 0; i < paraLength; i++) { 94 resultMyString.data[i] = data[paraStartPosition + i]; 95 } // Of for i 96 97 return resultMyString; 98 }// Of substring 99 100 /** 101 * The entrance of the program 102 * @param args Not used now. 103 */ 104 public static void main(String []args) { 105 MyString tempFirstString = new MyString("I like ik."); 106 MyString tempSecondString = new MyString("ik"); 107 int tempPosition = tempFirstString.locate(tempSecondString); 108 System.out.println("The position of \"" + tempSecondString + "\" in \"" + tempFirstString + "\" is: " + tempPosition); 109 110 MyString tempThirdString = new MyString("ki"); 111 tempPosition = tempFirstString.locate(tempThirdString); 112 System.out.println("The position of \"" + tempThirdString + "\" in \"" + tempFirstString + "\" is: " + tempPosition); 113 114 tempThirdString = tempFirstString.substring(1, 2); 115 System.out.println("The substring is: \"" + tempThirdString + "\""); 116 tempThirdString = tempFirstString.substring(5, 5); 117 System.out.println("The substring is: \"" + tempThirdString + "\""); 118 tempThirdString = tempFirstString.substring(5, 6); 119 System.out.println("The substring is: \"" + tempThirdString+ "\""); 120 121 }// Of main 122 123 }// Of MySting
标签:匹配,int,substring,tempThirdString,length,字符串,MyString,data,Day19 来源: https://www.cnblogs.com/f1uency/p/16385419.html