Remember The Word-Trie

简介: 题目: UVaLive 3942#include#includeusing namespace std;const int MAX = 4010 * 100; //题目中说过每个样例S

题目: UVaLive 3942

#include<cstdio>
#include<cstring>

using namespace std;

const int MAX = 4010 * 100;         //题目中说过每个样例S<4000 单个长度<100
const int MOD = 20071027;
int sz = 1;                         //用来记录每个字母的顺序
char str;
int ch[MAX][26];
int flag[MAX];                      //用来标识每个单词是否已经结束
int ans[300010];

void clearw(){
    sz = 1;    
    memset(ch[0],0,sizeof(ch[0]));
    memset(flag,0,sizeof(flag));
}
//创建一个新的字母节点
int creatw(int u){
    memset(ch[sz],0,sizeof(ch[sz]));
    flag[sz] = u;
    return  sz++;
}                       
/*插入函数:用来插入新的单词,如果在这一层已经存在这个字母了,继续往下摸,否则就
 *创建一个新的节点,在单词的最后设立一个结束的哨兵。这样建成一棵字典树。
 */
int insertw(char *s){
    int u = 0;
    int len = strlen(s);
    for(int i = 0;i < len;i++){
        int v = s[i] - 'a';
        if(!ch[u][v])
            ch[u][v] = creatw(0);
        u = ch[u][v];
    }
    flag[u] = 1;
}
/*查找函数:根据给定的深度和字符串开始搜索。依然是用s[i]-'a'来判断那个对应的位置
 *是不是符合条件,如果符合则这一层的就加上,因为所有这一层是不是有符合长度的字符串
 *都已经被压缩在flag中去了,所以只要flag是真,那就是可以相加,然后逐层相加,取模
 *即可
 */
void findw(int con,char *s){
    int u = 0;
    int deep = 0;
    for(; *s; s++){
        int v = *s - 'a';
        if(ch[u][v]){
            deep ++;
            u = ch[u][v];
            ans[con+deep] = (ans[con]*flag[u] + ans[con+deep])%MOD;
        }//因为要取MOD所以写成如此,不然也可以写成+=;
        else
            break;

    }
}

int main(){
    char str[300010];
    int cas = 1;
    while(~scanf("%s",str)){
        clearw();
        memset(ans,0,sizeof(ans));
        int len = strlen(str);
        ans[0] = 1;//这里非常重要,第一个一定是符合的
        int num;
        scanf("%d",&num);
        char strt[110];
        while(num--){
            scanf("%s",strt);
            insertw(strt);
        }
        for(int i = 1;i <= len;i++){
            findw(i-1,str+i-1);//因为是从最完整一个单词开始拆。
        }

        printf("Case %d: %d\n",cas++,ans[len]);
    }
    return  0;
}
相关文章
|
算法 Python
LeetCode 1160. 拼写单词 Find Words That Can Be Formed by Characters
LeetCode 1160. 拼写单词 Find Words That Can Be Formed by Characters
LeetCode 1160. 拼写单词 Find Words That Can Be Formed by Characters
LeetCode 429. N-ary Tree Level Order Traversal
给定一个 N 叉树,返回其节点值的层序遍历。 (即从左到右,逐层遍历)。
59 0
LeetCode 429. N-ary Tree Level Order Traversal
|
索引
LeetCode 373. Find K Pairs with Smallest Sums
给定两个以升序排列的整形数组 nums1 和 nums2, 以及一个整数 k。 定义一对值 (u,v),其中第一个元素来自 nums1,第二个元素来自 nums2。 找到和最小的 k 对数字 (u1,v1), (u2,v2) ... (uk,vk)。
124 0
LeetCode 373. Find K Pairs with Smallest Sums
LeetCode 212. Word Search II
给定一个二维网格 board 和一个字典中的单词列表 words,找出所有同时在二维网格和字典中出现的单词。 单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格。同一个单元格内的字母在一个单词中不允许被重复使用。
61 0
LeetCode 212. Word Search II
|
索引
LeetCode 79. Word Search
给定一个2D板和一个单词,找出该单词是否存在于网格中。 该单词可以由顺序相邻的单元的字母构成,其中“相邻”单元是水平或垂直相邻的单元。 相同的字母单元格不得多次使用。
54 0
LeetCode 79. Word Search
|
Android开发
The word is not correctly spelled问题
The word is not correctly spelled问题
187 0
The word is not correctly spelled问题
[LeetCode]--17. Letter Combinations of a Phone Number
Given a digit string, return all possible letter combinations that the number could represent. A mapping of digit to letters (just like on the telephone buttons) is given below. Input:D
1307 0
|
机器学习/深度学习
[LeetCode]--22. Generate Parentheses
Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. For example, given n = 3, a solution set is: [ "((()))", "(()())", "(())()",
1087 0