(step5.1.2)hdu 1305(Immediate Decodability——字典树)

149 篇文章 0 订阅
146 篇文章 1 订阅

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

如果是,输出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;
}



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

帅气的东哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值