题目链接:https://www.luogu.org/problemnew/show/P3808

#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 1000010;
int n, trie[maxn][27], next[maxn], tot, vis[maxn];
char s[maxn];
queue<int> q;
void _insert(char s[])
{
int now = 0;
int len = strlen(s);
for(int i = 0; i < len; i++)
{
int x = s[i]-'a';
if(!trie[now][x])
trie[now][x] = ++tot;
now = trie[now][x];
}
vis[now]++;
}
void get_next()
{
for(int i = 0; i < 26; i++)
{
if(trie[0][i])
{
next[trie[0][i]] = 0;
q.push(trie[0][i]);
}
}
while(!q.empty())
{
int cnt = q.front(); q.pop();
for(int i = 0; i < 26; i++)
{
if(trie[cnt][i])
{
next[trie[cnt][i]] = trie[next[cnt]][i];
q.push(trie[cnt][i]);
}
else
trie[cnt][i] = trie[next[cnt]][i];
}
}
}
int find(char s[])
{
int now = 0, ans = 0;
int len = strlen(s);
for(int i = 0; i < len; i++)
{
int x = s[i]-'a';
now = trie[now][x];
for(int k = now; k&&~vis[k]; k = next[k])
{
ans += vis[k];
vis[k] = -1;
}
}
return ans;
}
int main()
{
std::ios::sync_with_stdio(false);
cin>>n;
for(int i = 1; i <= n; i++)
{
cin>>s;
_insert(s);
}
get_next();
cin>>s;
cout<<find(s);
return 0;
}

最新文章

  1. 北京培训记day2
  2. codevs2645 Spore
  3. Alpha版本十天冲刺——Day 7
  4. css选择器选择顺序是从右往左的,为什么?
  5. Difference Between Vector and Deque in C++
  6. UVA247- Calling Circles(有向图的强连通分量)
  7. ng-cli
  8. A tutorial on Principal Components Analysis | 主成分分析(PCA)教程
  9. WP8.1应用双击返回键退出程序。
  10. STM32F10XX存储器细节
  11. undefined 与null的区别与差异
  12. SOA和微服务架构
  13. MinGW安装与使用简介
  14. C++程序调用python3
  15. Ubuntu 12.04上安装 MongoDB并运行
  16. oracle 启动em (使用浏览器打开)
  17. 概念:dependency injection, IOC, vs callback
  18. Windows XP Ghost系统安装
  19. 10W年薪和30W+年薪的产品经理差距在哪?
  20. c#按照指定长度切分字符串

热门文章

  1. jQuery对新添加的控件添加响应事件
  2. 2、弹出窗口 Alert
  3. 【学习笔记】HTML基础:列表、表格与媒体元素
  4. Hibernate=====HQL实用技术
  5. SQL使用bcp方式导入,导出数据2
  6. Java集合篇三:Vector
  7. stark——分页、search、actions
  8. 洛谷P3177 [HAOI2015]树上染色(树上背包)
  9. 使用 iframe + postMessage 实现跨域通信
  10. 三大集合框架之map