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

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

查找串t在串s中出現的次數

系統 2467 0

// 查找串t在串s中出現的次數,模式匹配,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中出現的次數


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

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

【本文對您有幫助就好】

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

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 扶余县| 肃南| 陕西省| 华容县| 新营市| 东至县| 宾阳县| 莲花县| 胶州市| 增城市| 广州市| 营口市| 双流县| 佛教| 饶平县| 沙雅县| 高邮市| 蕉岭县| 买车| 锦州市| 武城县| 西畴县| 开化县| 高安市| 揭西县| 蓬安县| 南康市| 峨眉山市| 晋州市| 奈曼旗| 五峰| 邢台县| 河东区| 蛟河市| 读书| 雷州市| 英山县| 阳高县| 合山市| 唐山市| 永嘉县|