码迷,mamicode.com
首页 > 其他好文 > 详细

字符串匹配

时间:2018-11-09 12:42:07      阅读:150      评论:0      收藏:0      [点我收藏+]

标签:pre   div   ash   margin   bre   mon   print   else   font   

判断str2中是否存在一个序列的集合 ix......im,使得str2(ix...im)=str1, ix不要求连续,且间隔的字符不能是str1中字符

str2="acsbassbba"

str1="abab"

不匹配

 

str2="acsbassba"

str1="abab"

匹配

 

 

对str1中出现的字符做一次hash记录.

找出所有str2中str1[0]字符的下标,for 循环此下标,如果出现不匹配,判断是不是str1的字符

 

 

public class Main
{

    public static void main(String[] args)
    {
        String str1 = "abab";
        String str2 = "acsbassbba";
        int a[] = new int[26];
        int b[] = new int[str2.length()];
        int bl = 0;
        for (int i = 0; i < str1.length(); i++)
            a[str1.charAt(0) - a] = 1;
        for (int i = 0; i < str2.length(); i++)
            if (str2.charAt(i) == str1.charAt(0))
            {
                b[bl++] = i;
            }
        int si = 1;
        for (int i? = b[0] + 1; i? < bl; i?++)
        {
            si = 1;
            for (int j = b[0] + 1; j < str2.length() && si != str1.length(); ++j)
            {
                if (str1.charAt(si) == str2.charAt(j))
                {
                    ++si;
                }
                else
                {
                    // 判断是否是str1中的字符
                    if (a[str2.charAt(j)-a] == 1)
                    {
                        // 错误的匹配
                        break;
                    }
                    // 不是j++
                }
            }
        }
        if (bl != 0 && si == str1.length()) System.out.println("ok");
        else
            System.out.println("false");
    }

}

 

字符串匹配

标签:pre   div   ash   margin   bre   mon   print   else   font   

原文地址:https://www.cnblogs.com/shuiyonglewodezzzzz/p/9934383.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!