匹配字符串问题

来源:互联网 发布:手机淘宝如何卖二手货 编辑:程序博客网 时间:2024/06/15 14:10

描述
Given two strings A and B, whose alphabet consist only ‘0’ and ‘1’. Your task is only to tell how many times does A appear as a substring of B? For example, the text string B is ‘1001110110’ while the pattern string A is ‘11’, you should output 3, because the pattern A appeared at the posit
输入
The first line consist only one integer N, indicates N cases follows. In each case, there are two lines, the first line gives the string A, length (A) <= 10, and the second line gives the string B, length (B) <= 1000. And it is guaranteed that B is always longer than A.

strstr(str,find) 查找
substr($res, 1)
php 代码如下

function match($str, $find){    $k = strlen($str);    if ($k > 1000)        return false;    if (strlen($find) > 10)        return false;    $count = 0;    for ($i = 0; $i < $k; $i++) {        $res = strstr($str, $find);        if ($res !== false) {            $str = substr($res, 1);            $count++;        } else {            break;        }    }    return $count = !$count ? false : $count;}
0 0
原创粉丝点击