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

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

否则输出Set .. is immediately decodable

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

解题思路:

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

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

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

package com.njupt.acm;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Scanner;
import java.util.Set; public class HDU_1305 { public static void main(String[] args) {
Scanner scanner = new Scanner(System.in); int counter = 1;
while (scanner.hasNext()) {
Set set = new HashSet();
String str = scanner.next();
boolean flag = true; set.add(str);
while (scanner.hasNext()) {
str = scanner.next();
if (str.equals("9")) {
if(flag){
System.out.println("Set "+(counter++)+" is immediately decodable");
}else{
System.out.println("Set "+(counter++)+" is not immediately decodable");
}
break;
}
Iterator<String> iter = set.iterator();
while (iter.hasNext()) {
String str1 = iter.next();
if (str.startsWith(str1) || str1.startsWith(str)) {
flag = false;
break;
}
}
if(flag){
set.add(str);
}
}
}
}
}

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

/*
* 1305_1.cpp
*
* Created on: 2013年8月24日
* Author: Administrator
*/ #include <iostream> using namespace std; const int maxn = 2;
struct node {
int count;
node* next[maxn];
}; node* root;
node* newset() {
node* current;
current = (node*) malloc(sizeof(node)); int i;
for (i = 0; i < maxn; ++i) {
current->next[i] = NULL;
} current->count = 1;
return current;
} void insert(char* s) {
node* current;
int len = strlen(s);
if (len == 0) {
return;
}
current = root; int i;
for (i = 0; i < len; ++i) {
if (current->next[s[i] - '0'] != NULL) {
current = current->next[s[i] - '0'];
current->count = current->count + 1;
} else {
current->next[s[i] - '0'] = newset();
current = current->next[s[i] - '0'];
}
}
} int find(char* s) {
node* current;
int len = strlen(s);
if (len == 0) {
return 0;
}
current = root; int i;
for (i = 0; i < len; ++i) {
if (current->next[s[i] - '0'] != NULL) {
current = current->next[s[i] - '0'];
} else {
return 0;
}
} return current->count;
} int main() {
char str[15][15];
int line = 0, count = 1; while (scanf("%s", str[line]) != EOF) {
root = newset();
line = 0;
bool flag = true;
insert(str[line++]);
while (scanf("%s", str[line]) != EOF) {
if (str[line][0] == '9') {
break;
}else{
insert(str[line++]);
} } int i;
for (i = 0; i < line; ++i) {
if (find(str[i]) > 1) {
flag = false;
break;
}
} if (flag) {
printf("Set %d is immediately decodable\n", count++);
} else {
printf("Set %d is not immediately decodable\n", count++);
} } delete root;
}

(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. 怎样在android实现uc和墨迹天气那样的左右拖动效果

    import android.app.Activity; import android.os.Bundle; import android.content.Context; import androi ...

  2. android开发隐藏了actionbar仍然短暂闪现的解决方法

    有时候我们在代码里隐藏了actionbar,在打开应用时,仍然短暂闪现下actionbar,用户体验很不好.   最简单的方法是 在AndroidManifest.xml中设置主题中配置不显示titl ...

  3. vtk基础编程(2)-读取数据文件中的坐标点

    原文地址: http://blog.csdn.net/chinamming/article/details/16860051 1. 案例说明 在实际计算中,常常需要大量的数据, 这个时候数据文件就必不 ...

  4. [置顶] oracle存储过程中单引号及字符串拼接处理

    在ORACLE中,单引号有两个作用,一是字符串是由单引号引用,二是转义.单引号的使用是就近配对,即就近原则.而在单引号充当转义角色时相对不好理解 1.从第二个单引号开始被视为转义符,如果第二个单引号后 ...

  5. C++中vector的实现

     注意几点: 分配内存不要使用new和delete,由于new的同一时候就把对象构造了.而我们须要的是原始内存. 所以应该使用标准库提供的allocator类来实现内存的控制.当然也能够重载ope ...

  6. JDK自己主动拆箱下,三目运算符的潜规则

    近期发现了一个非常诡异的NullPointerException,在以下这种方法抛出,一開始怎么都没想明确,dSrc即使为null,那直接赋值给distinct也没问题啊. private Doubl ...

  7. JAVA之File类创建对象构造函数传参数需要注意的几点

    java中File类用于创建一个文件对象. 首先看一段代码: 1. package MyText1; import java.io.File; public class MyText1 { publi ...

  8. CSDN改版问题多多

    刚刚上CSDN,发现改版了,推出C币功能. 然后看了2分钟,发现了一个Bug,于是准备提交到论坛.但是--居然提交Bug的论坛也出现Bug.印象中,每次CSDN更新版本号Bug都非常多,这,作为程序猿 ...

  9. [SVN] 分支同步、合入主干操作分享

    冲突的解决原则 不是自己修改的地方就使用主干的. 需要特别注意的是: 分支同步主干时,远端(theirs)是主干,本地(mine/working)的是分支: 分支合入主干时,本地(mine/worki ...

  10. jyphon 环境变量配置

    Jyphon 是基于java平台python 的一种实现 官网: http://www.jython.org/ 可以从官网下载 jyphon 安装 下载 jython Installer ,下载之后是 ...