题目大意:输入一系列的字符串,判断这些字符串中是否存在其中的一个字符串是另外一个字符串的前缀。。

如果是,输出Set 。。 is not immediately decodable

否则输出Set .. is immediately decodable

说的通俗点,就是判断一个字符串是否是两外一个字符串的前缀

解题思路:

这是一道字典树的题。一开始的时候,我用c/c++来写,然后是100行写完了,就是不知道哪里错了

这时,我实在忍不住了。直接就用java来写了

代码如下:(注意以下代码在submit的时候是需要对格式改一下的。。。但我这里可懒得改了)

  1. package com.njupt.acm;
  2.  
  3. import java.util.HashMap;
  4. import java.util.HashSet;
  5. import java.util.Iterator;
  6. import java.util.Map;
  7. import java.util.Scanner;
  8. import java.util.Set;
  9.  
  10. public class HDU_1305 {
  11.  
  12. public static void main(String[] args) {
  13. Scanner scanner = new Scanner(System.in);
  14.  
  15. int counter = 1;
  16. while (scanner.hasNext()) {
  17. Set set = new HashSet();
  18. String str = scanner.next();
  19. boolean flag = true;
  20.  
  21. set.add(str);
  22. while (scanner.hasNext()) {
  23. str = scanner.next();
  24. if (str.equals("9")) {
  25. if(flag){
  26. System.out.println("Set "+(counter++)+" is immediately decodable");
  27. }else{
  28. System.out.println("Set "+(counter++)+" is not immediately decodable");
  29. }
  30. break;
  31. }
  32. Iterator<String> iter = set.iterator();
  33. while (iter.hasNext()) {
  34. String str1 = iter.next();
  35. if (str.startsWith(str1) || str1.startsWith(str)) {
  36. flag = false;
  37. break;
  38. }
  39. }
  40. if(flag){
  41. set.add(str);
  42. }
  43. }
  44. }
  45. }
  46. }

以下附上让人心碎的c/c++代码

  1. /*
  2. * 1305_1.cpp
  3. *
  4. * Created on: 2013年8月24日
  5. * Author: Administrator
  6. */
  7.  
  8. #include <iostream>
  9.  
  10. using namespace std;
  11.  
  12. const int maxn = 2;
  13. struct node {
  14. int count;
  15. node* next[maxn];
  16. };
  17.  
  18. node* root;
  19. node* newset() {
  20. node* current;
  21. current = (node*) malloc(sizeof(node));
  22.  
  23. int i;
  24. for (i = 0; i < maxn; ++i) {
  25. current->next[i] = NULL;
  26. }
  27.  
  28. current->count = 1;
  29. return current;
  30. }
  31.  
  32. void insert(char* s) {
  33. node* current;
  34. int len = strlen(s);
  35. if (len == 0) {
  36. return;
  37. }
  38. current = root;
  39.  
  40. int i;
  41. for (i = 0; i < len; ++i) {
  42. if (current->next[s[i] - '0'] != NULL) {
  43. current = current->next[s[i] - '0'];
  44. current->count = current->count + 1;
  45. } else {
  46. current->next[s[i] - '0'] = newset();
  47. current = current->next[s[i] - '0'];
  48. }
  49. }
  50. }
  51.  
  52. int find(char* s) {
  53. node* current;
  54. int len = strlen(s);
  55. if (len == 0) {
  56. return 0;
  57. }
  58. current = root;
  59.  
  60. int i;
  61. for (i = 0; i < len; ++i) {
  62. if (current->next[s[i] - '0'] != NULL) {
  63. current = current->next[s[i] - '0'];
  64. } else {
  65. return 0;
  66. }
  67. }
  68.  
  69. return current->count;
  70. }
  71.  
  72. int main() {
  73. char str[15][15];
  74. int line = 0, count = 1;
  75.  
  76. while (scanf("%s", str[line]) != EOF) {
  77. root = newset();
  78. line = 0;
  79. bool flag = true;
  80. insert(str[line++]);
  81. while (scanf("%s", str[line]) != EOF) {
  82. if (str[line][0] == '9') {
  83. break;
  84. }else{
  85. insert(str[line++]);
  86. }
  87.  
  88. }
  89.  
  90. int i;
  91. for (i = 0; i < line; ++i) {
  92. if (find(str[i]) > 1) {
  93. flag = false;
  94. break;
  95. }
  96. }
  97.  
  98. if (flag) {
  99. printf("Set %d is immediately decodable\n", count++);
  100. } else {
  101. printf("Set %d is not immediately decodable\n", count++);
  102. }
  103.  
  104. }
  105.  
  106. delete root;
  107. }

(step5.1.2)hdu 1305(Immediate Decodability——字典树)的更多相关文章

  1. hdu 1305 Immediate Decodability(字典树)

    Immediate Decodability Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/O ...

  2. hdu 1671 Phone List 字典树

    // hdu 1671 Phone List 字典树 // // 题目大意: // // 有一些电话号码的字符串长度最多是10,问是否存在字符串是其它字符串的前缀 // // // 解题思路: // ...

  3. hdu 1305 Immediate Decodability

    原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1305 字典树裸题,如下: #include<algorithm> #include< ...

  4. hdu 1251 统计难题 (字典树入门题)

    /******************************************************* 题目: 统计难题 (hdu 1251) 链接: http://acm.hdu.edu. ...

  5. HDU 5536 Chip Factory 字典树

    Chip Factory Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid= ...

  6. HDU 1298 T9(字典树+dfs)

    http://acm.hdu.edu.cn/showproblem.php?pid=1298 题意:模拟手机9键,给出每个单词的使用频率.现在给出按键的顺序,问每次按键后首字是什么(也就是要概率最大的 ...

  7. HDU 2846 Repository(字典树,每个子串建树,*s的使用)

    Repository Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total ...

  8. HDU 1298 T9【字典树增加||查询】

    任意门:http://acm.hdu.edu.cn/showproblem.php?pid=1298 T9 Time Limit: 2000/1000 MS (Java/Others)    Memo ...

  9. HDU 5536 Chip Factory 字典树+贪心

    给你n个数,a1....an,求(ai+aj)^ak最大的值,i不等于j不等于k 思路:先建字典树,暴力i,j每次删除他们,然后贪心找k,再恢复i,j,每次和答案取较大的,就是答案,有关异或的貌似很多 ...

随机推荐

  1. Unity 3D 建立开发环境

    之后的基本方向 ios游戏开发,基础教程http://www.devdiv.com/unity_d_-thread-128068-1-1.html,学习Unity 3D游戏开发. 应该昨天表示,读了一 ...

  2. android的四种加载模式

    在Android中每个界面都是一个Activity,切换界面操作其实是多个不同Activity之间的实例化操作.在Android中Activity的启动模式决定了Activity的启动运行方式. An ...

  3. 获取wpf datagrid当前被编辑单元格的内容

    原文 获取wpf datagrid当前被编辑单元格的内容 确认修改单元个的值, 使用到datagrid的两个事件 开始编辑事件 BeginningEdit="dataGrid_Beginni ...

  4. Windows Phone 9再见了!

    因为需要准备注册电气工程师考试,因此Windows Phone 8初学者教程的翻译只能就此打住了,在考完后,也许就是Windows Phone 9发布的时候还会回来! Bye bye!

  5. Pro Android 4 第五章 理解Intent

         Android引入了一个名为Intent的概念用来唤醒各种组件.Android中的组件包括:activities(UI 组件),services(后台代码),broadcast receiv ...

  6. Extjs实现树形结构三连选

    当项目中需要一个部门人员选择或者省市县地域连选时,就需要树形结构的连选. 再此,写了一个简单的树形结构三连选功能,模拟从后台读取数据和处理数据(欢迎大家交流指正). 代码如下: 循环创建三棵树,其中只 ...

  7. Writing a Windows Shell Extension(marco cantu的博客)

    Writing a Windows Shell Extension This is a technical article covering the content of my last week s ...

  8. 写程序取自己进程的AEP

    测试程序功能 打印出自己进程的程序入口点地址. 结合OD载入程序,看到的入口点确实是0x004014f0, 说明程序入口点找到了 测试程序 /// @file exam_1_1.c #include  ...

  9. 动态面板——axure线框图部件库介绍

    1.什么是Axure的动态面板 按照Axure官方网站的解释 :动态面板控件(Dynamic Panel)可以让你实现高级的交互功能,实现原型的高保真度.动态面板包含有多个状态(states),每个状 ...

  10. Linux 安装Redis全过程日志

    wget http://download.redis.io/redis-stable.tar.gz tar xvzf redis-stable.tar.gz cd redis-stable make ...