当前位置:网站首页>Logu p4052 [jsoi2007] text generator solution
Logu p4052 [jsoi2007] text generator solution
2022-07-18 09:01:00 【q779】
Luogu P4052 [JSOI2007] Text generator Answer key
Topic link :P4052 [JSOI2007] Text generator
The question :
JSOI Give it to the team ZYX A task , Make a program called “ Text generator ” Computer software : The users of the software are young people , What they're using now is GW Text generator v6 edition .
The software can generate some articles randomly —— Always generate a fixed length and completely random article . in other words , Every character in the generated article is completely random . If an article contains at least one word that users know , So we say this article is readable ( We call it articles s s s Contains words t t t, If and only if the word t t t It's the article s s s The string of ). however , Even by this standard , What users use now GW Text generator v6 The article generated by the version is also almost completely unreadable .ZYX Need to point out that GW Text generator v6 In all text generated , Number of readable text , In order to be able to succeed in v7 Updated version . Can you help him ?
The answer is right 1 0 4 + 7 10^4 + 7 104+7 modulus .
For all test points , Guarantee :
- 1 ≤ n ≤ 60 1 \leq n \leq 60 1≤n≤60, 1 ≤ m ≤ 100 1 \leq m \leq 100 1≤m≤100.
- 1 ≤ ∣ s i ∣ ≤ 100 1 \leq |s_i| \leq 100 1≤∣si∣≤100, among ∣ s i ∣ |s_i| ∣si∣ Representation string s i s_i si The length of .
- s i s_i si It only contains capital English letters .
Obviously, it needs to be built A C \tt{AC} AC automata . Contains words , More trouble .
Consider tolerance and exclusion , Calculate the number of words not included ( Number of unreadable text )
「 Number of readable text 」 be equal to 「 Number of all texts 」 subtract 「 Number of unreadable text 」
「 Number of all texts 」 Obviously 2 6 m 26^m 26m
It's not hard to find out , An unreadable text in A C \tt{AC} AC When running on the automaton , You won't encounter 「 Dangerous nodes 」 Of
「 Dangerous nodes 」 Include 「 Is the end of the string 」 Node and 「 Some or some suffixes are words 」 The node of
The handling of dangerous nodes is relatively simple , Just deal with it when building automata , See code for details
Then this topic is counting , consider dp
set up f i , j f_{i,j} fi,j Said go j j j Node No , The length of the text is i i i When the number of programs
obviously f 0 , 0 = 1 f_{0,0} = 1 f0,0=1
f i + 1 , v = ∑ ( u , v ) ∈ E f i , u f_{i+1,v} = \sum_{(u,v) \in E} f_{i,u} fi+1,v=(u,v)∈E∑fi,u
there ( u , v ) ∈ E (u,v) \in E (u,v)∈E It means that A C \tt{AC} AC Automata exist u u u Point to v v v The edge of
「 Number of unreadable text 」 Is equal to ∑ 0 ≤ i ≤ tot f m , i \sum_{0 \le i \le \text{tot}} f_{m,i} ∑0≤i≤totfm,i
Time complexity O ( 26 × m ∑ ∣ s i ∣ ) O(26 \times m \sum |s_i|) O(26×m∑∣si∣)
Complete code :
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <iomanip>
#include <random>
#include <queue>
using namespace std;
#define int long long
#define INF 0x3f3f3f3f3f3f3f3f
#define N (int)(105)
#define L (int)(6e3+15)
const int p=1e4+7;
void add(int &a,int b){
a=(a%p+b%p)%p;}
int qpow(int a,int b)
{
int ans=1,base=a%p;
while(b)
{
if(b&1) ans=ans*base%p;
base=base*base%p;
b>>=1;
}
return ans;
}
struct Trie
{
queue<int> q;
int tot,trie[L][26],ed[L],fail[L],f[N][L];
void insert(string s)
{
int u=0;
for(int i=0; i<s.size(); i++)
{
int c=s[i]-'A';
if(!trie[u][c])trie[u][c]=++tot;
u=trie[u][c];
}
ed[u]=1;
}
void build()
{
for(int i=0; i<26; i++)
if(trie[0][i])q.push(trie[0][i]);
while(!q.empty())
{
int u=q.front(); q.pop();
for(int i=0; i<26; i++)
{
if(trie[u][i])
{
fail[trie[u][i]]=trie[fail[u]][i];
if(ed[fail[trie[u][i]]])ed[trie[u][i]]=1;
q.push(trie[u][i]);
}else trie[u][i]=trie[fail[u]][i];
}
}
}
void solve(int m)
{
f[0][0]=1;
for(int i=0; i<m; i++)
for(int j=0; j<=tot; j++)
for(int k=0; k<26; k++)
if(!ed[trie[j][k]])
add(f[i+1][trie[j][k]],f[i][j]);
int res=qpow(26,m);
for(int i=0; i<=tot; i++)
res=((res-f[m][i])%p+p)%p;
cout << res << '\n';
}
}tr;
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
// freopen("check.in","r",stdin);
// freopen("check.out","w",stdout);
int n,m; string s;
cin >> n >> m;
for(int i=1; i<=n; i++)
{
cin >> s;
tr.insert(s);
}
tr.build(); tr.solve(m);
return 0;
}
Reprint please explain the source
边栏推荐
- input device驱动流程
- [leetcode weekly -- hash table simulation] 6113 The smallest number in an infinite set
- (codeforce453)A.Little Pony and Expected Maximum(数学期望)
- 少儿编程中项目式学习的创造性
- 渲染与云渲染:一部电影的制作25%的时间是在“等”
- 使用buildroot学习驱动开发
- STM32与物联网02-网络数据收发
- Simple and not simple programming language go
- 字符设备之poll
- 在 SQL Server 中查找活动的 SQL 连接
猜你喜欢

《学习的底层逻辑》精华

九联科技开发板正式合入OpenHarmony主干

【LeetCode】9. Flood Fill·图像渲染

Allure测试报告怎么设置

base64和blob对图片的压缩

Inftnews | NFT tickets will change the way you participate in activities

行业首个「视频直播技术最佳实践图」发布!

【LeetCode】11. Lowest Common Ancestor of a Binary Search Tree· 二叉搜索树的最近公共祖先

MySQL about the installation process of zip installation package

实现有效的机器人教育培训管理模式
随机推荐
Grails 引发的中文乱码问题
iNFTnews | NFT门票将改变参与活动的方式
无需训练代码,推理性能提升1.4~7.1倍,业界首个自动模型压缩工具开源
SQLyog无操作一段时间后重新操作会卡死问题(解决办法)
(2021 Niuke multi school III) j-counting triangles (thinking)
(codeforce453)A.Little Pony and Expected Maximum(数学期望)
字符设备之poll
金融、生物医药、集成电路,上海巨头行业用AI加持核心竞争力
GET 请求和 POST 请求的区别和使用
[2023 school recruitment questions] column planning (non final presentation status, for bloggers' personal reference)
解析结合劳动教育的steam新课程
Without training code, the reasoning performance is improved by 1.4 ~ 7.1 times, and the industry's first automatic model compression tool is open source!
渲染与云渲染:一部电影的制作25%的时间是在“等”
Honghu Wanlian Zhiyuan development board is officially integrated into the openharmony backbone
【MATLAB项目实战】基于GUI的数字信号处理系统
[leetcode weekly -- string] 6114 Move the clip to get the string
Google Earth Engine APP(GEE)—查看亚马逊平原的1984——至今的每一景影像
qt制作颜色选择控件
Online sql to yaml tool
[Halcon] WriteImage保存图像崩溃问题