• UVAL 3942 Remember the Word(递推+Trie)


    Remember the Word

    【题目链接】Remember the Word

    【题目类型】递推+Trie

    &题解:

    蓝书P209,参考的别人公开代码

    &代码:

    #include <cstdio>
    #include <bitset>
    #include <iostream>
    #include <set>
    #include <cmath>
    #include <cstring>
    #include <algorithm>
    #include <map>
    #include <queue>
    #include <vector>
    using namespace std;
    #define INF 0x3f3f3f3f
    typedef long long ll;
    const int maxn= 4005*100 +7,sigmaSize=26;
    int ch[maxn][sigmaSize],sz,K;
    bool vis[maxn];
    void init() {sz=1; memset(ch[0],0,sizeof(ch[0]));}
    void add(char *s) {
    	int u=0;
    	for(int i=0; s[i]; i++) {
    		int c=s[i]-'a';
    		if(!ch[u][c]) {
    			memset(ch[sz],0,sizeof(ch[sz]));
    			vis[sz]=false;
    			ch[u][c]=sz++;
    		}
    		u=ch[u][c];
    	}
    	vis[u]=true;
    }
    const int LEN=3e5+5,mod=20071027;
    char str[LEN];
    int d[LEN];
    int main() {
    	freopen("E:1.in","r",stdin);
    	char word[120];
    	while(~scanf("%s",str)) {
    		init();
    		int S; scanf("%d",&S);
    		while(S--) {
    			scanf("%s",word);
    			add(word);
    		}
    		int n=0;
    		while(str[n]) d[n++]=-1;
    		d[n]=1;
    		for(int cur=n-1; cur>=0; cur--) {
    			int u=0;
    			d[cur]=0;
    			for(int i=cur; i<n; i++) {
    				int c=str[i]-'a';
    				if(!ch[u][c]) break;
    				u=ch[u][c];
    				if(vis[u]) d[cur]=(d[cur]+d[i+1])%mod;
    			}
    		}
    		printf("Case %d: %d
    ",++K,d[0]);
    	}
    	return 0;
    }
    
  • 相关阅读:
    Linux安装Docker
    Api接口防攻击防刷注解实现
    Api接口鉴权注解实现
    RSA加解密 Java
    Windows安装Mysql 5.7
    Mysql创建自增序列
    new String与toString的区别
    各排序算法复杂度及稳定性
    描述快排以及其复杂度
    innodb和myisam的区别
  • 原文地址:https://www.cnblogs.com/s1124yy/p/6705854.html
Copyright © 2020-2023  润新知