HDOJ/HDU 1113 Word Amalgamation(字典顺序~Map)
Problem Description
In millions of newspapers across the United States there is a word game called Jumble. The object of this game is to solve a riddle, but in order to find the letters that appear in the answer it is necessary to unscramble four words. Your task is to write a program that can unscramble words.
Input
The input contains four parts:
- a dictionary, which consists of at least one and at most 100 words, one per line;
- a line containing XXXXXX, which signals the end of the dictionary;
- one or more scrambled `words’ that you must unscramble, each on a line by itself; and
- another line containing XXXXXX, which signals the end of the file.
All words, including both dictionary words and scrambled words, consist only of lowercase English letters and will be at least one and at most six characters long. (Note that the sentinel XXXXXX contains uppercase X’s.) The dictionary is not necessarily in sorted order, but each word in the dictionary is unique.
Output
For each scrambled word in the input, output an alphabetical list of all dictionary words that can be formed by rearranging the letters in the scrambled word. Each word in this list must appear on a line by itself. If the list is empty (because no dictionary words can be formed), output the line “NOT A VALID WORD” instead. In either case, output a line containing six asterisks to signal the end of the list.
Sample Input
tarp
given
score
refund
only
trap
work
earn
course
pepper
part
XXXXXX
resco
nfudre
aptr
sett
oresuc
XXXXXX
Sample Output
score
******
refund
******
part
tarp
trap
******
NOT A VALID WORD
******
course
******
题意:
输入字典 XXXXXX结束字典的输入 然后输入字符串 如果字符串能够组成字典中的串就输出该串 否则输出NOT A VALID WORD
就是找到字典中与其相同字母构成的字符串。
(找到的字符串如果有很多,要按照字典顺序输出!)
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
/**
* @author 陈浩翔
* 2016-5-27
*/
public class Main{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
String str="";
String chStr="";
Map<String , List<String>> map = new HashMap<String, List<String>>();
while(true){
List<String> list = new ArrayList<String>();
str=sc.next();
if("XXXXXX".equals(str)){
break;
}
char ch[] = str.toCharArray();
Arrays.sort(ch);
chStr=new String(ch);
if(map.get(chStr)==null){
list.add(str);
map.put(chStr, list);
}else{
list = map.get(chStr);
list.add(str);
map.put(chStr, list);
}
}
while(true){
str=sc.next();
if("XXXXXX".equals(str)){
break;
}
char ch[] = str.toCharArray();
Arrays.sort(ch);
chStr=new String(ch);
List<String> list = map.get(chStr);
if(list==null){
System.out.println("NOT A VALID WORD");
}else{
String strs[] = new String[list.size()];
for(int i=0;i<list.size();i++){
strs[i]=list.get(i);
}
Arrays.sort(strs, new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
return o1.compareTo(o2);
}
});
for(int i=0;i<strs.length;i++){
System.out.println(strs[i]);
}
}
System.out.println("******");
}
}
}
}
HDOJ/HDU 1113 Word Amalgamation(字典顺序~Map)的更多相关文章
- HDU 1113 Word Amalgamation (map 容器 + string容器)
http://acm.hdu.edu.cn/showproblem.php?pid=1113 Problem Description In millions of newspapers across ...
- HDOJ.1113 Word Amalgamation(map)
Word Amalgamation 点我挑战题目 点我一起学习STL-MAP 题意分析 给出字典.之后给出一系列======乱序======单词,要求你查字典,如过这个乱序单词对用有多个有序单词可以输 ...
- hdu 1113 Word Amalgamation
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1113 字符串简单题: stl水过 如下: #include<algorithm> #inc ...
- hdu - 1113 Word Amalgamation (stl)
http://acm.hdu.edu.cn/showproblem.php?pid=1113 给定一个字典,然后每次输入一个字符串问字典中是否有单词与给定的字符串的所有字母一样(顺序可以打乱),按字典 ...
- hdu 1113 Word Amalgamation 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1113 题意:输入一个字典,然后再输入若干单词(每行中,1 <= 单词数 <= 100,并且 ...
- hdu Word Amalgamation(map)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1113 找单词 #include <iostream> #include <strin ...
- hdu1113 Word Amalgamation(详解--map和string的运用)
版权声明:本文为博主原创文章.未经博主同意不得转载. vasttian https://blog.csdn.net/u012860063/article/details/35338617 转载请注明出 ...
- poj1318 Word Amalgamation 字符串排序(qsort)
Word Amalgamation Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 9794 Accepted: 4701 ...
- poj 1318 Word Amalgamation
Word Amalgamation Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 9968 Accepted: 4774 ...
随机推荐
- 13_输出映射1_resultType
输出映射主要有两种:resultType和resultMap [resultType] 可以返回三种类型 pojo对象:例如select * from user where id=? pojo对象列表 ...
- 九度OJ 1371 最小的K个数 -- 堆排序
题目地址:http://ac.jobdu.com/problem.php?pid=1371 题目描述: 输入n个整数,找出其中最小的K个数.例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4 ...
- python 自动化之路 day 02
本节内容: 列表.元组操作 字符串操作 1. 列表.元组操作 列表是我们最以后最常用的数据类型之一,通过列表可以对数据实现最方便的存储.修改等操作 定义列表 1 names = ['Alex',&qu ...
- js hover放大效果
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- php static 关键字在 函数中的用法
至于在类中声明为 static 的属性和方法,这里不描述php中的变量作用范围的另一个重要特性就是静态变量(static 变量).静态变量仅在局部函数域中存在且只被初始化一次,当程序执行离开此作用域时 ...
- js监听
IE浏览器监听: function attachEvent(string eventFlag, function eventFunc) eventFlag: 事件名称,但要加上on,如onclick. ...
- php重载
重载 PHP所提供的"重载"(overloading)是指动态地"创建"类属性和方法.我们是通过 魔术方法(magic methods)来实现的. 当调用当前环 ...
- mysqli 取出数据库中某表的表头和内容
需求如题 取出数据库中某表的表头和内容,并显示该表的行数和列数 <?php //显示表内容的函数 function showTable($tableName){ //连接数据库 $mysqli= ...
- windows 远程桌面连接ubuntu xrdp 只看到墙纸其他什么都没有
用 windows 的 mstsc 连接 ubuntu 的 xrdp 时,进入后只看到墙纸,其他什么都没有,鼠标指针也不见,输入按键都无反应. 原来 Ubuntu 启动了 3d 桌面,导致 xrdp ...
- armv7a-mediatek451_001_vfp-linux-gnueabi-gcc: directory: No such file or directory 编译error
release/vm_linux/output/hisense_android/mt5399_cn_android_JB/rel/obj/oss/source/arm_mali_ko/mali400- ...