抄代码DAY12 字符串匹配

简单的字符串匹配算法,输入子串(模式串),求其在主串中的位置。一旦发现这个子串的字符不匹配,只能转而从主串的下一个来匹配。

先进行字符串的定义并进行初始化

    public static final int MAX_LENGTH = 10;//定义字符串最大长度

	int length;//定义字符串的实际长度
	char[] data;//字符串的值

	//初始化定义一个空的字符串
	public MyString() {
		length = 0;
		data = new char[MAX_LENGTH];
	}

	public MyString(String paraString) {
		data = new char[MAX_LENGTH];
		length = paraString.length();//字符串的长度

		//复制数据
		for (int i = 0; i < length; i++) {
			data[i] = paraString.charAt(i);
		} 
	}

重写公共子类toString

	//重写公共类中的方法
	public String toString() {
		String resultString = "";

		for (int i = 0; i < length; i++) {
			resultString += data[i];
		} 

		return resultString;
	}

1.定位子串在主串内的位置

利用tempMatch来标记是否完成匹配,如果ture表示匹配成功,如果是false表示匹配失败。默认情况下为false,当主串和子串匹配时,将tempMatch为ture,否则标记为false。

暴力匹配,先读取主串第一个字符,和子串第一个字符进行比较,若成功,就比较下一个;不成功,继续读取主串第二个字符,然后再和子串第一个字符进行比较......以此类推,直至全部匹配成功。

	//定位字符在字符串内部的位置
	public int locate(MyString paraMyString) {
		boolean tempMatch = false;
		for (int i = 0; i < length - paraMyString.length + 1; i++) {
			//初始化
			tempMatch = true;
			for (int j = 0; j < paraMyString.length; j++) {
				if (data[i + j] != paraMyString.data[j]) {
					tempMatch = false;
					break;
				} //对子串进行匹配
			} 

			if (tempMatch) {
				return i;
			} // 返回匹配位置
			
		} 
		return -1;
	}

2.输入数值返回在主串中对应位置的子串

输入第几个元素,在主串中查找返回其值,返回值存在新定义的reasultMyString中。先检查是否越界。再在主串中进行查找(利用for循环),返回对应值。

	public MyString substring(int paraStartPosition, int paraLength) {
		if (paraStartPosition + paraLength > length) {
			System.out.println("The bound is exceeded.");
			return null;
		} //检查是否越界

		MyString resultMyString = new MyString();//定义返回值子串
		resultMyString.length = paraLength;//定义子串长度
		for (int i = 0; i < paraLength; i++) {
			resultMyString.data[i] = data[paraStartPosition + i];
		} // 赋值给返回值

		return resultMyString;
	}

main方法

	public static void main(String args[]) {
		MyString tempFirstString = new MyString("Lionel Messi is the best football player in the world.");
		MyString tempSecondString = new MyString("Messi");
		int tempPosition = tempFirstString.locate(tempSecondString);
		System.out.println("The position of \"" + tempSecondString + "\" in \"" + tempFirstString
				+ "\" is: " + tempPosition);

		MyString tempThirdString = new MyString("play");
		tempPosition = tempFirstString.locate(tempThirdString);
		System.out.println("The position of \"" + tempThirdString + "\" in \"" + tempFirstString
				+ "\" is: " + tempPosition);

		tempThirdString = tempFirstString.substring(1, 2);
		System.out.println("The substring is: \"" + tempThirdString + "\"");

		tempThirdString = tempFirstString.substring(5, 5);
		System.out.println("The substring is: \"" + tempThirdString + "\"");

		tempThirdString = tempFirstString.substring(5, 6);
		System.out.println("The substring is: \"" + tempThirdString + "\"");
	}

结果为

实现上述操作。

总结:不同字符串注意更改MAX_LENGTH值。本例的子串比较简单,但是如果遇到较长的子串,建议使用KMP算法。

暴力匹配算法在每次匹配失败都是模式后移一位再从头开始比较。但是前面可能有大部分相同。除了这种暴力匹配,还有KMP算法。利用“移动位数=已匹配的字符串-对应的部分匹配值”,来进行移动,设置next数组进行辅助。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值