这题我开始想的简单了,WA一次,然后看disscuss里有人说输入时长度从小到大的,然后我信了。然后开始while(1) WA;然后我尝试先放如数组。后来对了;

discuss里面果然不能太相信。

根据出现的次数来判断是否为前缀。

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct trie
{
trie *next[];
int sum;
};
trie *root;
void creattrie()
{
root=(trie*)malloc(sizeof(trie));
for(int i=;i<;i++)
{
root->next[i]=NULL;
}
root->sum=;
}
void insert(char *str)
{
int i,j,cnt=;
int len=strlen(str);
trie *p=root,*q;
for(i=;i<len;i++)
{
int id=str[i]-'';
if(p->next[id]==NULL)
{
q=(trie*)malloc(sizeof(trie));
for(j=;j<;j++)
q->next[j]=NULL;
q->sum=;
p->next[id]=q;
}
p=p->next[id];
p->sum++;
}
} int query(char *str)
{
int i,j;
int cnt=;
int len=strlen(str);
trie *p=root;
for(i=;i<len;i++)
{
int id=str[i]-'';
p=p->next[id];
}
if(p->sum>)//判断到该字符串结束时,现在的sum是否超过2,如果是,那就代表后面有以这个为前缀的词
cnt=;
if(cnt)
return ;
return ;
} int main()
{
int i,j,flag,ff=,ret,count;
char str[][];
while(gets(str[])!=NULL)
{
if(str[][]=='')break;
count=;
creattrie();
insert(str[]);
while(gets(str[count]))
{
if(str[count][]=='')break;
insert(str[count]);
count++;
}
flag=;
for(i=;i<count;i++)
{
flag=query(str[i]);
if(flag)break;
}
if(flag)printf("Set %d is not immediately decodable\n",++ff);
else printf("Set %d is immediately decodable\n",++ff);
}
return ;
}

hdu1305 字典树的更多相关文章

  1. hdu1305 字典树水题

    题意:      给你一些字符串,然后问你他们中有没有一个串是另一个串的前缀. 思路:       字典树水题,(这种水题如果数据不大(这个题目不知道大不大,题目没说估计不大),hash下也行,把每个 ...

  2. 3道入门字典树例题,以及模板【HDU1251/HDU1305/HDU1671】

    HDU1251:http://acm.hdu.edu.cn/showproblem.php?pid=1251 题目大意:求得以该字符串为前缀的数目,注意输入格式就行了. #include<std ...

  3. HDU1305 Immediate Decodability(水题字典树)

    巧了,昨天刚刚写了个字典树,手到擒来,233. Problem Description An encoding of a set of symbols is said to be immediatel ...

  4. HDU1305 Immediate Decodability (字典树

    Immediate Decodability An encoding of a set of symbols is said to be immediately decodable if no cod ...

  5. 萌新笔记——用KMP算法与Trie字典树实现屏蔽敏感词(UTF-8编码)

    前几天写好了字典,又刚好重温了KMP算法,恰逢遇到朋友吐槽最近被和谐的词越来越多了,于是突发奇想,想要自己实现一下敏感词屏蔽. 基本敏感词的屏蔽说起来很简单,只要把字符串中的敏感词替换成"* ...

  6. [LeetCode] Implement Trie (Prefix Tree) 实现字典树(前缀树)

    Implement a trie with insert, search, and startsWith methods. Note:You may assume that all inputs ar ...

  7. 字典树+博弈 CF 455B A Lot of Games(接龙游戏)

    题目链接 题意: A和B轮流在建造一个字,每次添加一个字符,要求是给定的n个串的某一个的前缀,不能添加字符的人输掉游戏,输掉的人先手下一轮的游戏.问A先手,经过k轮游戏,最后胜利的人是谁. 思路: 很 ...

  8. 萌新笔记——C++里创建 Trie字典树(中文词典)(一)(插入、遍历)

    萌新做词典第一篇,做得不好,还请指正,谢谢大佬! 写了一个词典,用到了Trie字典树. 写这个词典的目的,一个是为了压缩一些数据,另一个是为了尝试搜索提示,就像在谷歌搜索的时候,打出某个关键字,会提示 ...

  9. 山东第一届省赛1001 Phone Number(字典树)

    Phone Number Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 We know that if a phone numb ...

随机推荐

  1. hdu 5800 To My Girlfriend + dp

    传送门:hdu 5800 To My Girlfriend 题意:给定n个物品,其中i,j必选,l,m必不选,问组成体积为s的方法一共有多少种 思路:定义dp[i][j][s1][s2],表示前i种物 ...

  2. 如何禁止 iPhone Safari video标签视频自动全屏?

    最近做一个移动端微信页面项目,在微信页面中有视频播放,但是需要禁止IOS的自动全屏播放(前提必须使用video标签).如: <video id="post" autoplay ...

  3. 第2章 面向对象的设计原则(SOLID):6_开闭原则

    6. 开闭原则(Open Closed Principle,OCP) 6.1 定义 (1)一个类应该对扩展开放,对修改关闭.要求通过扩展来实现变化,而且是在不修改己有的代码情况下进行扩展,也不必改动己 ...

  4. css3实现立方体的旋转功能

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. linux下的缓存机制及清理buffer/cache/swap的方法梳理

    (1)缓存机制 为了提高文件系统性能,内核利用一部分物理内存分配出缓冲区,用于缓存系统操作和数据文件,当内核收到读写的请求时,内核先去缓存区找是否有请求的数据,有就直接返回,如果没有则通过驱动程序直接 ...

  6. 【转】【C#】C#重绘windows窗体标题栏和边框

    摘要 windows桌面应用程序都有标准的标题栏和边框,大部分程序也默认使用这些样式,一些对视觉效果要求较高的程序,如QQ, MSN,迅雷等聊天工具的样式则与传统的windows程序大不相同,其中迅雷 ...

  7. C语言 const与指针

    //const与指针 #include<stdio.h> #include<stdlib.h> void main(){ ; ; const int *p; p = & ...

  8. GSON

    { "data": [ { "children": [ { "id": 10007, "title": "北京 ...

  9. C中的预编译宏定义

     可以用宏判断是否为ARC环境 #if _has_feature(objc_arc) #else //MRC #endif C中的预编译宏定义 -- 作者: infobillows 来源:网络 在将一 ...

  10. [CareerCup] 9.1 Climbing Staircase 爬楼梯

    9.1 A child is running up a staircase with n steps, and can hop either 1 step, 2 steps, or 3 steps a ...