Remember the Word UVALive - 3942 DP_字典树
2024-08-31 13:37:06
每个小单词的长度都是小于等于100的,这是个重要的突破口.
Code:
#include <cstdio>
#include <algorithm>
#include <cstring>
#define setIO(s) freopen(s".in","r",stdin)
#define maxn 300006
#define mod 20071027
using namespace std;
char str[maxn],wd[maxn];
int tag[maxn],n,f[maxn],m,cas;
struct TRIE{
#define SIGMA 30
int root,ch[maxn][SIGMA],cnt;
void init() {
root = cnt = 0;
memset(ch,0,sizeof(ch));
memset(tag,0,sizeof(tag));
memset(f,0,sizeof(f));
}
void ins(char s[]) {
int len = strlen(s),p = root;
for(int i = 0;i < len; ++i) {
if(!ch[p][s[i]-'a']) ch[p][s[i]-'a'] = ++cnt;
p = ch[p][s[i]-'a'];
}
tag[p] = 1;
}
void solve(int st){
int p = root;
for(int i = st;i <= m; ++i) {
p = ch[p][str[i]-'a'];
if(!p) break;
if(tag[p]) f[st] += f[i + 1],f[st] %= mod;
if(tag[p] && i == m) f[st] += 1;
}
}
}trie;
int main(){
//setIO("input");
while(scanf("%s",str) != EOF) {
scanf("%d",&n),trie.init(),m = strlen(str) - 1;
for(int i = 1;i <= n; ++i) scanf("%s",wd),trie.ins(wd);
for(int i = m;i >= 0; --i) trie.solve(i);
printf("Case %d: %d\n",++cas,f[0]);
}
return 0;
}
最新文章
- Amazon Interview | Set 27
- 备忘录--关于线程和IO知识
- Linux 网络编程详解十
- PHP多表取数据的代码优化
- Java异常与运行时异常,以及与线程的关系
- HDU1013Digital Roots
- [转]Response.AddHeader 文本下载
- <;<;深入Java虚拟机>;>;-第三章-垃圾收集器与内存分配策略-学习笔记
- javascript下用ActiveXObject控件替换word书签,将内容导出到word后打印第1/2页
- PHP设计模式笔记八:原型模式 -- Rango韩老师 http://www.imooc.com/learn/236
- github上传文件
- Python Scrapy项目创建(基础普及篇)
- mariadb安装
- Confluence 6 管理协同编辑 - 最大编辑者的限制
- MySQL学习笔记1(增删查改)
- centos7构建python2.7常用开发环境
- 看病要排队--hdu1873
- Python 不定参数函数
- VS2015快捷键及常用功能
- Mysql查询架构信息