ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Day19 字符串匹配

2022-06-17 13:33:03  阅读:130  来源: 互联网

标签:匹配 int substring tempThirdString length 字符串 MyString data 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码。
  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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有