洛谷 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
", ans);
	}
	return 0;
}

End

本文来自博客园,作者:xixike,转载请注明原文链接:https://www.cnblogs.com/xixike/p/15111415.html

原文地址:https://www.cnblogs.com/xixike/p/15111415.html