1. 程式人生 > 其它 >洛谷 P3966 [TJOI2013]單詞

洛谷 P3966 [TJOI2013]單詞

洛谷 P3966 [TJOI2013]單詞

原題連結

Solution

AC自動機

洛谷 P5337 \(AC\)自動機(二次加強版)裸題

不多說了,看我部落格吧,有詳解

洛谷 P5357 【模板】AC自動機(二次加強版)

不知道上面的部落格有沒有看懂呢?

看不懂沒關係,看下面\(\downarrow\)

find 函式

食用方法:\(t.find(s, pos)\)

在字串 \(t\) 的第 \(pos\) 位開始查詢字串 \(s\) 出現的位置(第一個字元),如果沒有,返回 -1。

完整程式碼

#include <iostream>
#include <cstdio>
#include <cstring>
#include<algorithm>

using namespace std;

const int N = 2e5 + 10;
int n;
string s[N], t;

int main(){
	scanf("%d", &n);
	for(int i=1;i<=n;i++)
		cin >> s[i];
	cin >> t;
	for(int i = 1; i <= n; i++){
		int ans = 0;
		int pos = t.find(s[i], 0);
		while(pos != -1){
			cout<<"pos "<<pos<<endl;
			pos = t.find(s[i], pos + 1);
			ans++;
		}
		printf("%d\n", ans);
	}
	return 0;
}

End