当前位置:网站首页>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
边栏推荐
- According to the analysis of the Internet industry in 2022, how to choose a suitable position?
- Make Jar, Not War
- [signal and system]
- Long press the button to execute the function
- 公钥\私人 ssh避password登陆
- JS ES5也可以創建常量?
- Mysqlbackup restores specific tables
- Clickhouse fields are grouped and aggregated, and SQL is queried according to the granularity of any time period
- Appium基础 — Appium Inspector定位工具(一)
- 搭建【Redis in CentOS7.x】
猜你喜欢

1123. 最深叶节点的最近公共祖先

Make Jar, Not War

Start from the bottom structure to learn the customization and testing of fpga---- FIFO IP

Can't you understand the code of linked list in C language? An article allows you to grasp the secondary pointer and deeply understand the various forms of parameter passing in the function parameter

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

Dark horse notes - exception handling

Yunna - work order management system and process, work order management specification

The difference between Tansig and logsig. Why does BP like to use Tansig

百度飞将BMN时序动作定位框架 | 数据准备与训练指南 (下)

Instructions for using the domain analysis tool bloodhound
随机推荐
Machine learning: the difference between random gradient descent (SGD) and gradient descent (GD) and code implementation.
golang 基础 —— 数据类型
各种语言,软件,系统的国内镜像,收藏这一个仓库就够了: Thanks-Mirror
Mysqlbackup restores specific tables
C语言实例_5
C语言实例_2
js如何快速创建一个长度为 n 的数组
curl 命令
JS ES5也可以創建常量?
AcWing 1142. Busy urban problem solving (minimum spanning tree)
安全保护能力是什么意思?等保不同级别保护能力分别是怎样?
编译命令行终端 swift
Set WordPress pseudo static connection (no pagoda)
[advanced C language] 8 written questions of pointer
swiper组件中使用video导致全屏错位
[signal and system]
数据手册中的词汇
Instructions for using the domain analysis tool bloodhound
ZOJ problem set – 2563 long dominoes [e.g. pressure DP]
前置机是什么意思?主要作用是什么?与堡垒机有什么区别?