hdu1251题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1251

统计难题


Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/Others)
Total Submission(s): 65063    Accepted Submission(s): 22438

Problem Description

Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己的前缀).

Input

输入数据的第一部分是一张单词表,每行一个单词,单词的长度不超过10,它们代表的是老师交给Ignatius统计的单词,一个空行代表单词表的结束.第二部分是一连串的提问,每行一个提问,每个提问都是一个字符串.

注意:本题只有一组测试数据,处理到文件结束.

Output

对于每个提问,给出以该字符串为前缀的单词的数量.

Sample Input

banana
band
bee
absolute
acmba
b
band
abc

Sample Output

2 3 1 0

解题思路:


在建字典树的时候用一个数组sum[]记录每个节点被访问过的次数,这样就可以在查询的时候找到该节点输出该节点的sum[]值即可

ac代码:


#include <iostream>
#include <algorithm>
#include <cstring>
#include <ctype.h>
#include <set>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <fstream>
typedef long long ll;
const int maxn=2e6+5;
using namespace std;
int tot=0;//总节点数
int tree[maxn][30],words[maxn],sum[maxn];
string s,ss;
void insert()
{int root=0;for(int i=0;i<s.length();i++){int id=s[i]-'a';if(!tree[root][id]) tree[root][id]=++tot;root=tree[root][id];sum[root]++;//记录节点访问次数}//words[root]=1;//单词标记
}
int query(string ss)
{int len=ss.length(),root=0;for(int i=0;i<len;i++){int id=ss[i]-'a';if(!tree[root][id]) return 0;//没有出现过root=tree[root][id];}return sum[root];
}
int main()
{//freopen("/Users/zhangkanqi/Desktop/11.txt","r",stdin);while(getline(cin,s)){if(s[0]=='\0')//或者写成if(s.length()==0){while(cin>>ss)printf("%d\n",query(ss));}else insert();//建树}return 0;
}

poj2001题目地址:http://poj.org/problem?id=2001

题目:


Shortest Prefixes

Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 22927   Accepted: 9737

Description

求唯一标识一个单词的前缀,这个前缀可以是单词本身,如样例中的car

Sample Input

carbohydrate
cart
carburetor
caramel
caribou
carbonic
cartilage
carbon
carriage
carton
car
carbonate

Sample Output

carbohydrate carboh
cart cart
carburetor carbu
caramel cara
caribou cari
carbonic carboni
cartilage carti
carbon carbon
carriage carr
carton carto
car car
carbonate carbona

解题思路:


先建立字典树,在建树过程中标记每个节点出现的次数,在查询的时候如何第一碰到只出现一次的节点或者已经到了这个单词的末尾,那么就输出相应的结果

ac代码:


#include <bits/stdc++.h>
typedef long long ll;
const int maxn=1e6+5;
using namespace std;
int tot=0;
int tree[maxn][30],cnt[maxn];
string s;
void build(string s)
{int root=0,len=s.length();for(int i=0;i<len;i++){int id=s[i]-'a';if(!tree[root][id]) tree[root][id]=++tot;root=tree[root][id];cnt[root]++;}cnt[root]++;
}
string query(string s)
{int len=s.length(),root=0;string ans="";for(int i=0;i<len;i++){int id=s[i]-'a';ans+=s[i];root=tree[root][id];if(ans==s || cnt[root]==1)//单词return ans;}
}
int main() {//freopen("/Users/zhangkanqi/Desktop/11.txt", "r", stdin);queue<string> q;while (cin >> s){build(s);q.push(s);}while(!q.empty()){string str=q.front();q.pop();cout<<str<<" "<<query(str)<<endl;}return 0;
}

POJ3630题目地址: http://poj.org/problem?id=3630

题目:


                                                                          Phone List

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 36604   Accepted: 10408

Description

Given a list of phone numbers, determine if it is consistent in the sense that no number is the prefix of another. Let's say the phone catalogue listed these numbers:

  • Emergency 911
  • Alice 97 625 999
  • Bob 91 12 54 26

In this case, it's not possible to call Bob, because the central would direct your call to the emergency line as soon as you had dialled the first three digits of Bob's phone number. So this list would not be consistent.

Sample Input

2
3
911
97625999
91125426
5
113
12340
123440
12345
98346

Sample Output

NO
YES

ac代码:


#include <bits/stdc++.h>
typedef long long ll;
const int maxn=1e5;
using namespace std;
int tot=0;
int tree[maxn][30],cnt[maxn],words[maxn];
string s;
bool ans=true;
void build(string s)
{int root=0,len=s.length();for(int i=0;i<len;i++){int id=s[i]-'0';if(!tree[root][id]) tree[root][id]=++tot;root=tree[root][id];cnt[root]++;}words[root]=1;
}
bool query()
{for(int i=1;i<=tot;i++)if(words[i] && cnt[i]>1)return false;return true;
}
int main() {//freopen("/Users/zhangkanqi/Desktop/11.txt", "r", stdin);int t,n;cin>>t;while(t--){cin>>n;ans=true;tot=0;memset(words,0,sizeof(words));memset(tree,0,sizeof(tree));memset(cnt,0,sizeof(cnt));while(n--){cin>>s;build(s);}printf("%s\n",query()?"YES":"NO");}return 0;
}

【HDU1251+POJ2001+POJ3630】单词前缀问题整理相关推荐

  1. 【快乐水题】2000. 反转单词前缀

    原题: 力扣链接:2000. 反转单词前缀 题目简述: 给你一个下标从 0 开始的字符串 word 和一个字符 ch .找出 ch 第一次出现的下标 i ,反转 word 中从下标 0 开始.直到下标 ...

  2. 第 258 场周赛(5867. 反转单词前缀/ 5868. 可互换矩形的组数 / 5869. 两个回文子序列长度的最大乘积(状态压缩) / 5870. 每棵子树内缺失的最小基因值(小大合并))

    第 258 场周赛 从这次开始还是把题目加上名字吧,要不然想找找不到题,哈哈 字节的周赛,还想冲个200来着,最后一题按二叉树写的,用例还能过,一提交,发现多叉的...来不及改了..400多名 586 ...

  3. Trie树统计单词前缀

    输入 输入的第一行为一个正整数n.表示词典的大小,其后n行,每一行一个单词(不保证是英文单词,也有可能是火星文单词哦).单词由不超过10个的小写英文字母组成,可能存在同样的单词.此时应将其视作不同的单 ...

  4. java单词匹配算法_前端学数据结构与算法(八): 单词前缀匹配神器-Trie树的实现及其应用...

    前言 继二叉树.堆之后,接下来介绍另外一种树型的数据结构-Trie树,也可以叫它前缀树.字典树.例如我们再搜索引擎里输入几个关键字之后,后续的内容会自动续上.此时我们输入的关键词也就是前缀,而后面的就 ...

  5. 英语单词 前缀词根后缀

    对于一个单词,是由前缀+词根+词汇,组成.在此基础上如果是动词就有时态变化,如果是名词就有单数和负数的变化.快速的记住一串单词,应该去解析去积累词根前缀和词词缀. 参考一个视频: [学好英语?看这一个 ...

  6. Java常用英语单词(自己整理)(更新中...)

    自己日常使用的单词,放在这里也是为了方便观看,为了日后的更新的方便,只要遇到新的都会更新到这里,如果有错误的欢迎指出来. 序号 英文 标准注解 代码中注解 1 norm 标准,规范 2 priorit ...

  7. LeetCode简单题之反转单词前缀

    题目 给你一个下标从 0 开始的字符串 word 和一个字符 ch .找出 ch 第一次出现的下标 i ,反转 word 中从下标 0 开始.直到下标 i 结束(含下标 i )的那段字符.如果 wor ...

  8. leetcode算法题--反转单词前缀

    原题链接:https://leetcode-cn.com/problems/reverse-prefix-of-word/ class Solution {public:string reverseP ...

  9. LeetCode 2000. 反转单词前缀

    文章目录 1. 题目 2. 解题 1. 题目 给你一个下标从 0 开始的字符串 word 和一个字符 ch . 找出 ch 第一次出现的下标 i ,反转 word 中从下标 0 开始.直到下标 i 结 ...

  10. 别再说你不会ElasticSearch,都给你整理好了

    点击上方"朱小厮的博客",选择"设为星标" 后台回复"书",获取 后台回复"k8s",可领取k8s资料 本文不会关注 E ...

最新文章

  1. 系统异常和不稳定的原因总结
  2. Python cv2 摄像头
  3. intellij idea14.1 卡顿问题解决
  4. python画图显示中文乱码_解决Python pandas plot输出图形中显示中文乱码问题
  5. HACMP环境修改IP的方法
  6. 图解Picasso源码
  7. 电脑当路由使用(目前只在win7上用过)
  8. Selenium学习(11) 网页截图
  9. python正则表达式笔记之字符集合的使用
  10. SPSS 问卷与量表的区别及联系【SPSS 030期】
  11. 北邮2017计算机考研题,北邮通信考研2017年801真题.pdf
  12. 用python怎样解偏微分方程组_用Python数值求解偏微分方程
  13. mfc利用SQL、DAO调用access数据库
  14. JavaScript知识点整理(十三)- DOM -(2)操作元素
  15. ai替换混合轴例子_可解释的vs可解释的AI:一个直观的例子
  16. Android 调用系统相机并加时间水印
  17. 云主机、云服务器、VPS的区别性能比较
  18. 圆锥形怎么画_草图大师怎么画圆锥形?
  19. win下连编socket时[Linker error] undefined reference to XXX
  20. 计算机调剂还是找工作,儿子是C9学生,考研只能调剂,究竟是调剂好还是找工作好?...

热门文章

  1. 再续上一篇:如果哪天沃尔玛也“.CN”了
  2. 剑指offer、把二叉树打印成多行(python)
  3. 【Docker】06 DockerFile
  4. avs php,linux 安装AdultVideoScript (AVS)全教程
  5. 使用html的a标签,无法转跳到servlet页面中的解决办法,适用于servlet的初学者,不与框架相兼容
  6. Java中继承方法的使用,以及使用方法的注意事项,继承方法重写的理解,白话文详解,简单易理解
  7. mysql点击计数器_高性能Mysql(第3版)_网站点击计数器
  8. 【转】vue中如何实现数据的双向绑定
  9. Zephyr学习(一)Zephyr介绍
  10. 基于共享内存和多重哈希实现分布式缓存系统