当前位置:网站首页>uva 1401 dp+Trie
uva 1401 dp+Trie
2022-07-06 17:56:00 【全栈程序员站长】
大家好,又见面了,我是全栈君,今天给大家准备了Idea注册码。
http://uva.onlinejudge.org/index.php?
option=com_onlinejudge&Itemid=8&page=show_problem&category=&problem=4147
题意:给定一个字符串,以及若干单词,求有几种方式能用单词组成字符串
我先是dp方程推得有问题不知怎么改动搞得卡了非常久,然后就是数组开得太小一直RE
trie数组大小=单词个数*单词长度
dp[i]为以str[i]开头的后缀的ans。dp[i]=segma(dp[k]),当中k表示str[i…k-1]是一个单词。假设k=len,那么dp[i]++;
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
#define ll long long
const int N =4000*100+100;
const int MOD =20071027;
char str[300019],pat[115];
ll dp[300019];
const int tk=26,tb='a';
int top,tree[N][tk+1],len;
void init()
{
top=1;
memset(tree[0],0,sizeof(tree[0]));
}
int sear(char*s,int i)
{
int cnt=0;
ll ans=0;
for(int rt=0;rt=tree[rt][*s-tb] ;++s)
{
if(*(s)==0)break;
cnt++;
if(tree[rt][tk])//cnt!=tree[rt][tk]表示dp[i..len-1]是一个单词,此时没有添加切割的种数
{
if(*(s+1)==0)ans++;
ans=(ans+dp[i+cnt])%MOD;
//////////////////////
//printf("rt=%d s=%s i=%d cnt=%d dp=%lld\n",rt,str+i+cnt,i,cnt,dp[i]);
//////////////////////
}
}
return ans;
}
void Insert(char*s, int Rank=0)//Rank为长度
{
int rt,nxt;
for(rt=0;*s;rt=nxt,++s,Rank++)
{
nxt=tree[rt][*s-tb];
if(0 == nxt)//nxt!=0的时候就是有公共前缀了。已经在之前做过了,仅仅需继续跳转即可he中插入her,到h,e都是nxt!=0不用插入
{
tree[rt][*s-tb]=nxt=top;
memset(tree[top],0,sizeof(tree[top]));
top++;
}
}
tree[rt][tk]=Rank;
}
int main()
{
//freopen("uva1401.txt","r",stdin);
int n,ncase=1,pos;
while(scanf("%s",str)!=EOF)
{
init();
pos=0;
len=strlen(str);
scanf("%d",&n);
for(int i=0;i<n;i++)
{
scanf("%s",pat);
Insert(pat);
}
dp[len]=0;
for(int i=len-1;i>=0;i--)
{
dp[i]=0;
dp[i]=sear(str+i,i);
}
printf("Case %d: %lld\n",ncase++,dp[0]);
}
return 0;
}版权声明:本文博主原创文章。博客,未经同意不得转载。
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/116891.html原文链接:https://javaforall.cn
边栏推荐
- Wood extraction in Halcon
- docker 方法安装mysql
- 分享一个通用的so动态库的编译方法
- Atomic in golang and CAS operations
- Can the system hibernation file be deleted? How to delete the system hibernation file
- 从零开始匹配vim(0)——vimscript 简介
- How to prevent overfitting in cross validation
- 域分析工具BloodHound的使用说明
- Realize incremental data synchronization between MySQL and ES
- THREE.AxesHelper is not a constructor
猜你喜欢

让我们,从头到尾,通透网络I/O模型

HMM 笔记

The MySQL database in Alibaba cloud was attacked, and finally the data was found

Do you understand this patch of the interface control devaxpress WinForms skin editor?

系统休眠文件可以删除吗 系统休眠文件怎么删除

阿里云中mysql数据库被攻击了,最终数据找回来了

云呐|工单管理软件,工单管理软件APP

子网划分、构造超网 典型题

【信号与系统】

LeetCode:1175. Prime permutation
随机推荐
MySQL script batch queries all tables containing specified field types in the database
AI automatically generates annotation documents from code
Dark horse notes - exception handling
黑马笔记---异常处理
子网划分、构造超网 典型题
系统休眠文件可以删除吗 系统休眠文件怎么删除
实现mysql与ES的增量数据同步
THREE. AxesHelper is not a constructor
Metauniverse urban legend 02: metaphor of the number one player
一起看看matlab工具箱内部是如何实现BP神经网络的
[100 cases of JVM tuning practice] 05 - Method area tuning practice (Part 2)
Yunna - work order management system and process, work order management specification
身体质量指数程序,入门写死的小程序项目
C语言实例_5
LLDP兼容CDP功能配置
NEON优化:矩阵转置的指令优化案例
Receive user input, height BMI, BMI detection small business entry case
ARM裸板调试之JTAG调试体验
Amway wave C2 tools
How to manage distributed teams?