实现一个挺高级的字符匹配算法: 给一串很长字符串,要求找到符合要求的字符串,例如目的串:123 1******3***2 ,12*****3这些都要找出来

java实现:

思路来源于:http://blog.csdn.net/cxllyg/article/details/7595878


package rand;


public class WordsFilter {

//for-loop to match O=mn, 判断是否存在这样的字符      方法一
public static boolean wordsFilter_1(String target,String source){

int lenT = target.length();
int lenS = source.length();
int j=0;
for(int i=0;i<lenT;i++)
for(j=0;j<lenS;j++)
       if(target.charAt(i)==source.charAt(j))
        break;
   if(j>=lenS)return false;
return true;
}


//use hash table to match     判断是否存在这样的字符      方法二
public static boolean wordsMatch(int[]arr_1,int[]arr_2){
int len1=arr_1.length;
int len2 = arr_2.length;
for(int i=0;i<len1;i++)
for(int j=0;j<len2;j++)
        if(arr_2[arr_1[i]]<=0)
        return false;
return true;


}
//create hash table
public static void createTable(String target,String source,int[]arr_1,int[]arr_2){
int len1 = target.length();
int len2 = source.length();

for(int i=0;i<len1;i++)
arr_1[i]=(int)target.charAt(i);
for(int j=0;j<len2;j++)
arr_2[(int)source.charAt(j)]++;
}


//match words and find all in words   找出全部字符
/***

* @param args
* 伪代码:
*   target source
*   len = target.lenth
*   for i form 0 len
*      hashTanle[target[i]]++;
*   while(index<source.ength)
*       if source[index] in hashTable != 0
*          if source[index] in cnt(Array)==0
*             count++;
*             cnt[]++;
*             if len == count:
*                 while(true)
*                          cnt[sorce[front]]--;
*                          if(cnt[source[front]]==0)
*                             for j from front to rear
*                                 print source[font] to source[rear]
*                             if(rear - front+1<right-left+1)
*                                left = front
*                                right = rear
*                             for j from 0 to 255
*                                 cnt[j]=0
*                             count =0;
*                             front++;
*             
*                          front++;
*           else
*             cnt[i]++
*       rear++;
*       
*       
*   j from left to right
*     print source[j]
*   
*/


public static void wordsFilter(String target,String source){

int lenOfT = target.length();
int lenOfS = source.length();
int start=0,count=0,left = 0,
right = Integer.MAX_VALUE;
int[] hs = new int[256];
int[] store = new int[256];
// create hashTable
for(int j=0;j<lenOfT;j++){
hs[target.charAt(j)]=1;
}

// count the different character in target
lenOfT = 0;
for(int i =0;i<256;i++){
            if(hs[i]>0)
            lenOfT++;
}

   for(int i =0;i<lenOfS;i++){
       if(hs[source.charAt(i)]==1){
       
        if(store[source.charAt(i)]==0){
        count++;
        store[source.charAt(i)]++;
       
        if(count==lenOfT){
        for(int j=start;j<=i;j++){
        if(store[source.charAt(j)]!=0){
        store[source.charAt(i)]--;
        if(store[source.charAt(i)]==0){
        // print character
        for(int k=j;k<=i;k++)
        System.out.print(source.charAt(k));
        System.out.println();
       
           if(right-left>i-j){
            left = j;
            right = i;
           }
           
           for(int k=0;k<256;k++){
            store[k]=0;
           }
           count=0;
           start=i+1;
           break;
        }
        }
        }
         }
       
           }
        else
        {
        store[source.charAt(i)]++;
        }
       
       }  
   
   }
   // print shortest character
   if(right<Integer.MAX_VALUE){
    for(int i=left;i<=right;i++)
      System.out.print(source.charAt(i));
   }
}




//client
public static void main(String[]args){
//init
String target ="123";
String source="143256df31j27fd1fd452gy36";
int[]arr_1 = new int[target.length()];
int[] arr_2 = new int[256];


//method2
System.out.println(wordsFilter_1(target,source));

//method2
createTable(target,source,arr_1,arr_2);
System.out.println(wordsMatch(arr_1,arr_2));

//find all child String
wordsFilter(target,source);
}

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值