当前位置:网站首页>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
边栏推荐
- Make Jar, Not War
- Receive user input, height BMI, BMI detection small business entry case
- Your cache folder contains root-owned files, due to a bug in npm ERR! previous versions of npm which
- Yunna - work order management system and process, work order management specification
- 云呐-工单管理制度及流程,工单管理规范
- Dynamic planning idea "from getting started to giving up"
- Spark TPCDS Data Gen
- Match VIM from zero (0) -- Introduction to vimscript
- 2022 Google CTF segfault Labyrinth WP
- 子网划分、构造超网 典型题
猜你喜欢
Dynamic planning idea "from getting started to giving up"
Lldp compatible CDP function configuration
[case sharing] basic function configuration of network loop detection
Analysis of mutex principle in golang
一起看看matlab工具箱内部是如何实现BP神经网络的
JTAG debugging experience of arm bare board debugging
Make Jar, Not War
Byte P7 professional level explanation: common tools and test methods for interface testing, Freeman
JS reverse -- ob confusion and accelerated music that poked the [hornet's nest]
UI control telerik UI for WinForms new theme - vs2022 heuristic theme
随机推荐
移植DAC芯片MCP4725驱动到NUC980
The difference between spin and sleep
Docker method to install MySQL
[case sharing] basic function configuration of network loop detection
Gnet: notes on the use of a lightweight and high-performance go network framework
Clickhouse fields are grouped and aggregated, and SQL is queried according to the granularity of any time period
THREE. AxesHelper is not a constructor
golang中的atomic,以及CAS操作
ARM裸板调试之JTAG调试体验
【js】获取当前时间的前后n天或前后n个月(时分秒年月日都可)
taro3.*中使用 dva 入门级别的哦
Gazebo的安装&与ROS的连接
736. Lisp 语法解析 : DFS 模拟题
1123. The nearest common ancestor of the deepest leaf node
Dark horse notes - create immutable sets and streams
ClickHouse字段分组聚合、按照任意时间段粒度查询SQL
C language instance_ two
Boot - Prometheus push gateway use
云呐|工单管理办法,如何开展工单管理
子网划分、构造超网 典型题