当前位置:网站首页>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
边栏推荐
猜你喜欢
Appium基础 — Appium Inspector定位工具(一)
Appium automation test foundation uiautomatorviewer positioning tool
LeetCode:1175. Prime permutation
制作带照明的DIY焊接排烟器
Dark horse notes - create immutable sets and streams
Yunna | work order management measures, how to carry out work order management
Yunna - work order management system and process, work order management specification
405 method not allowed appears when the third party jumps to the website
Gin introduction practice
Let's see how to realize BP neural network in Matlab toolbox
随机推荐
Instructions for using the domain analysis tool bloodhound
7.6 simulation summary
长按按钮执行函数
Clickhouse fields are grouped and aggregated, and SQL is queried according to the granularity of any time period
What does security capability mean? What are the protection capabilities of different levels of ISO?
拖拽改变顺序
Mysqlbackup restores specific tables
C language - array
新工作感悟~辞旧迎新~
Gin introduction practice
What does front-end processor mean? What is the main function? What is the difference with fortress machine?
域分析工具BloodHound的使用说明
AcWing 345. 牛站 题解(floyd的性质、倍增)
JS Es5 can also create constants?
云呐|工单管理办法,如何开展工单管理
Start from the bottom structure to learn the customization and testing of fpga---- FIFO IP
Table table setting fillet
剑指 Offer II 035. 最小时间差-快速排序加数据转换
mysqlbackup 还原特定的表
THREE. AxesHelper is not a constructor