我是靠谱客的博主 甜美白昼,这篇文章主要介绍【字符串】序列自动机,现在分享给大家,希望可以做个参考。

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include<bits/stdc++.h> #define ll long long #define rep(i,a,n) for(int i=a;i<=n;i++) #define per(i,n,a) for(int i=n;i>=a;i--) #define endl 'n' #define mem(a) memset(a,0,sizeof(a)) #define IO ios::sync_with_stdio(false);cin.tie(0); using namespace std; const int INF=0x3f3f3f3f; const int mod=1e9+7; const int maxn=1e5+10; char str[maxn],s[maxn]; int nxt[maxn][30]; void getnext(){ memset(nxt,0,sizeof(nxt)); int len=strlen(str+1); for(int i=len;i>=1;i--){ for(int j=0;j<26;j++){ nxt[i-1][j]=nxt[i][j]; } nxt[i-1][str[i]-'a']=i; } } int main(){ scanf("%s",str+1); getnext(); int T;cin>>T; getchar(); while(T--){ int flag=1; scanf("%s",s); int len=strlen(s);int now=0; for(int i=0;i<len;i++){ now=nxt[now][s[i]-'a']; if(!now){ flag=0;break; } } if(flag) puts("YES"); else puts("NO"); } }

另一种dp做的自动机,给你长度为n的字符串序列,求出长度为k的子序列有几种不重复的:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include<bits/stdc++.h> #define ll long long #define rep(i,a,n) for(int i=a;i<=n;i++) #define per(i,n,a) for(int i=n;i>=a;i--) #define endl 'n' #define mem(a) memset(a,0,sizeof(a)) #define IO ios::sync_with_stdio(false);cin.tie(0); using namespace std; const int INF=0x3f3f3f3f; const int maxn=1e3+5; const int mod=1e9+7; int dp[maxn][maxn],pos[60]; char s[maxn]; int main(){ int n,k;cin>>n>>k; scanf("%s",s+1); dp[0][0]=1; for(int i=1;i<=n;i++){ dp[i][0]=1; for(int j=1;j<=k;j++){ dp[i][j]=dp[i-1][j]+dp[i-1][j-1]; if(pos[s[i]-'a']) dp[i][j]-=dp[pos[s[i]-'a']-1][j-1]; dp[i][j] = (dp[i][j]%mod+mod)%mod; } pos[s[i]-'a']=i; } cout<<dp[n][k]<<endl; }

最后

以上就是甜美白昼最近收集整理的关于【字符串】序列自动机的全部内容,更多相关【字符串】序列自动机内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(87)

评论列表共有 0 条评论

立即
投稿
返回
顶部