当前位置:网站首页>uva 1401 dp+Trie
uva 1401 dp+Trie
2022-07-07 01:42:00 【Full stack programmer webmaster】
Hello everyone , I meet you again , I'm the king of the whole stack , I've prepared for you today Idea Registration code .
http://uva.onlinejudge.org/index.php?
option=com_onlinejudge&Itemid=8&page=show_problem&category=&problem=4147
The question : Given a string , And some words , There are several ways to use words to form a string
First I was dp There was a problem with the equation. I don't know how to change it. It got stuck for a long time , Then the array is opened too small and always RE
trie Array size = The number of words * Word length
dp[i] For str[i] Beginning with suffix ans.dp[i]=segma(dp[k]), among k Express str[i…k-1] It's a word . hypothesis k=len, that 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] Express dp[i..len-1] It's a word , At this time, the number of cutting types is not added
{
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 For the length
{
int rt,nxt;
for(rt=0;*s;rt=nxt,++s,Rank++)
{
nxt=tree[rt][*s-tb];
if(0 == nxt)//nxt!=0 When there is a public prefix . It has been done before , Just continue to jump he Insert her, To h,e All are nxt!=0 Don't insert
{
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;
}Copyright notice : This article is the original article of the blogger . Blog , Do not reprint without permission .
Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/116891.html Link to the original text :https://javaforall.cn
边栏推荐
- 云呐|工单管理办法,如何开展工单管理
- AcWing 1142. 繁忙的都市 题解(最小生成树)
- Hutool post requests to set the body parameter to JSON data
- 黑马笔记---创建不可变集合与Stream流
- Start from the bottom structure to learn the customization and testing of fpga---- FIFO IP
- Appium automation test foundation uiautomatorviewer positioning tool
- Telnet,SSH1,SSH2,Telnet/SSL,Rlogin,Serial,TAPI,RAW
- Your cache folder contains root-owned files, due to a bug in npm ERR! previous versions of npm which
- WCF Foundation
- 百度飞将BMN时序动作定位框架 | 数据准备与训练指南 (下)
猜你喜欢

Appium foundation - appium inspector positioning tool (I)

鼠标右键 自定义

【C语言进阶篇】指针的8道笔试题

Instructions for using the domain analysis tool bloodhound

Gin 入门实战

C语言关于链表的代码看不懂?一篇文章让你拿捏二级指针并深入理解函数参数列表中传参的多种形式

405 method not allowed appears when the third party jumps to the website

Byte P7 professional level explanation: common tools and test methods for interface testing, Freeman

我如何编码8个小时而不会感到疲倦。
![[signal and system]](/img/aa/a65d6da1d1d9410254ca7b775e24a6.png)
[signal and system]
随机推荐
Hutool post requests to set the body parameter to JSON data
Make Jar, Not War
图片打水印 缩放 和一个输入流的转换
糊涂工具类(hutool)post请求设置body参数为json数据
[chip scheme design] pulse oximeter
AcWing 1142. 繁忙的都市 题解(最小生成树)
Typical problems of subnet division and super network construction
Dark horse notes - exception handling
Amway wave C2 tools
机器学习:随机梯度下降(SGD)与梯度下降(GD)的区别与代码实现。
JS es5 peut également créer des constantes?
dvajs的基础介绍及使用
交叉验证如何防止过拟合
When grep looks for a process, it ignores the grep process itself
JS ES5也可以创建常量?
454 Baidu Mianjing 1
Taro applet enables wxml code compression
Drag to change order
C language instance_ four
1123. 最深叶节点的最近公共祖先