(step5.1.2)hdu 1305(Immediate Decodability——字典树)
题目大意:输入一系列的字符串,判断这些字符串中是否存在其中的一个字符串是另外一个字符串的前缀。。
如果是,输出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——字典树)的更多相关文章
- hdu 1305 Immediate Decodability(字典树)
Immediate Decodability Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/O ...
- hdu 1671 Phone List 字典树
// hdu 1671 Phone List 字典树 // // 题目大意: // // 有一些电话号码的字符串长度最多是10,问是否存在字符串是其它字符串的前缀 // // // 解题思路: // ...
- hdu 1305 Immediate Decodability
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1305 字典树裸题,如下: #include<algorithm> #include< ...
- hdu 1251 统计难题 (字典树入门题)
/******************************************************* 题目: 统计难题 (hdu 1251) 链接: http://acm.hdu.edu. ...
- HDU 5536 Chip Factory 字典树
Chip Factory Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid= ...
- HDU 1298 T9(字典树+dfs)
http://acm.hdu.edu.cn/showproblem.php?pid=1298 题意:模拟手机9键,给出每个单词的使用频率.现在给出按键的顺序,问每次按键后首字是什么(也就是要概率最大的 ...
- HDU 2846 Repository(字典树,每个子串建树,*s的使用)
Repository Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total ...
- HDU 1298 T9【字典树增加||查询】
任意门:http://acm.hdu.edu.cn/showproblem.php?pid=1298 T9 Time Limit: 2000/1000 MS (Java/Others) Memo ...
- HDU 5536 Chip Factory 字典树+贪心
给你n个数,a1....an,求(ai+aj)^ak最大的值,i不等于j不等于k 思路:先建字典树,暴力i,j每次删除他们,然后贪心找k,再恢复i,j,每次和答案取较大的,就是答案,有关异或的貌似很多 ...
随机推荐
- MongoDB系列之二(主动复制)
目前我正在进行MongoDB的双机热备方面相关的工作.根据我目前看到的MongoDB方面的材料,MongoDB的实际部署有三种方式,分别是“主动复制”,“副本集”以及“分片副本集”. 首先我们从最简单 ...
- MDK的优化应用
MDK的优化应用 http://blog.163.com/zhaojun_xf/blog/static/300505802011291384721/ 使用Keil/MDK这么多年了,一直都没有使用它的 ...
- C语言,题目:函数调用,内存,malloc找错
malloc int* p = (int *) malloc (sizeof(int)*128); //分配128个(可根据实际需要替换该数值)整型存储单元,并将这128个连续的整型存储单元的首地址存 ...
- 使用boost中的property_tree实现配置文件
property_tree是专为配置文件而写,支持xml,ini和json格式文件 ini比较简单,适合简单的配置,通常可能需要保存数组,这时xml是个不错的选择. 使用property_tr ...
- 13-(1-4)进程管道关于popen(-r-w)及pipe的程序使用实例
#include<unistd.h> #include<stdlib.h> #include<stdio.h> #include<string.h> # ...
- java 基于JDK中的源码总结下String二
申明:转载请注明出处,如有商用目的请务必知会本人,感谢. 上一篇文章:http://blog.csdn.net/ts1122/article/details/8738336,介绍了String一些易错 ...
- 程序员必须知道的几个Git代码托管平台(转)
上一篇博客中2015继续任性——不会Git命令,照样玩转Git我们简单的介绍了在VS2013中使用Git,和GitHub客户端的使用.那么使用Git到底有什么好处呢?最为明显的是支持Git代码托管的平 ...
- 怎样使用 App Studio 高速定制你自己的 Universal Windows App
今天之所以在写一篇关于 App Studio 的文章是由于,App Studio 经过了几次升级功能得到了明显提升还能够调用系统功能了.而且能够更方便的和应用商店关联公布 Universal Wind ...
- GetCursorPos/WindowFromPoint/SendMessage
GetCursorPos/WindowFromPoint/SendMessage (用API函数向Edit框发送字符) GetCursorPos(mPoint); DTWND:=WindowFromP ...
- Thinkphp入门 二 —空操作、空模块、模块分组、前置操作、后置操作、跨模块调用(46)
原文:Thinkphp入门 二 -空操作.空模块.模块分组.前置操作.后置操作.跨模块调用(46) [空操作处理] 看下列图: 实际情况:我们的User控制器没有hello()这个方法 一个对象去访问 ...