由暴力匹配算法想到

暴力匹配是最基本最直接的匹配算法,从前到后逐一匹配,若失配,则字符串右移一格,重头再来。

用SAS实现就是:

 1 %let text=abcdadbdftee;
 2 %let find=ft;
 3 data _null_;
 4     a="&text";
 5     b="&find";
 6     len_a=length(a);
 7     len_b=length(b);
 8     i=1;
 9     j=1;
10     do while (i<=len_a & j<=len_b);
11         a1=substr(a,i,1);
12         b1=substr(b,j,1);
13         if a1=b1 then do;
14             i++1;j++1;
15         end;
16         else if a1 ne b1 then do;
17             i=i-j+2;j=1;
18         end;
19     end;
20     if j>len_b then do;
21         position=i-j+1;
22         put "------POSITION:" position "---";
23     end;
24     put _all_;
25 run;

这种方法比较古老,也比较"不先进"。

 

鄙人利用SAS的特性,写了一个不超过10行的代码实现此功能,分享给大家:

1 %let text=abcdadbdftee;
2 %let find=ft;
3 data _null_;
4     do i=1 to %length(&text)-%length(&find)+1;
5         want=substr("&text",i,%length(&find));
6         if want="&find" then put "---POSITION:" i "---";
7     end;
8 run;

 

欢迎大家拍砖

 

by yant07

 

posted @ 2016-06-29 18:02  yant07  阅读(226)  评论(0编辑  收藏  举报