#include#includeusingnamespacestd;strings,t;vectornext;voidnextIt()...{inti=0,j=-1,n=t.size();next[0]=-1;while(i

日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

查找串t在串s中出現(xiàn)的次數(shù)

系統(tǒng) 2661 0

// 查找串t在串s中出現(xiàn)的次數(shù),模式匹配,KMP算法
#include < iostream >
#include
< string >
#include
< vector >
using namespace std;

string s,t;
vector
< int > next;

void nextIt()
... {
int i = 0 ,j =- 1 ,n = t.size();
next[
0 ] =- 1 ;

while (i < n)
... {
if (j ==- 1 || t[i] == t[j])
... {
i
++ ;
j
++ ;
next[i]
= j;
}

else
j
= next[j];
}

}


int countIt()
... {
int i = 0 ,j = 0 ,cnt = 0 ,n = s.size(),m = t.size();
while (i < n)
... {
if (j ==- 1 || s[i] == t[j])
... {
i
++ ;
j
++ ;
if (j == m)
... {
cnt
++ ;
j
= next[j];
}

}

else
j
= next[j];
}

return cnt;
}


void run()
... {
cin
>> t >> s;
next.resize(t.size()
+ 1 );
nextIt();
cout
<< countIt() << endl;
}


int main()
... {
int n;
cin
>> n;
for ( int i = 0 ;i < n;i ++ )run();
return 0 ;
}

查找串t在串s中出現(xiàn)的次數(shù)


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 东阳市| 伊宁县| 邳州市| 越西县| 白朗县| 逊克县| 晋江市| 新密市| 南丰县| 大丰市| 台北县| 犍为县| 冕宁县| 右玉县| 安陆市| 明星| 鹤峰县| 元朗区| 丽水市| 孟津县| 台湾省| 沙河市| 闽清县| 富宁县| 呼伦贝尔市| 蓝田县| 正定县| 凤城市| 将乐县| 卢氏县| 高青县| 寻乌县| 上饶县| 利津县| 辽中县| 印江| 宁明县| 连城县| 奉化市| 隆德县| 茂名市|