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:

  1. a dictionary, which consists of at least one and at most 100 words, one per line;
  2. a line containing XXXXXX, which signals the end of the dictionary;
  3. one or more scrambled `words’ that you must unscramble, each on a line by itself; and
  4. 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("******");
            }
        }
    }
}
时间: 2024-07-31 13:52:44

HDOJ/HDU 1113 Word Amalgamation(字典顺序~Map)的相关文章

UVa 642 Word Amalgamation:查字典&amp;amp;字符串排序

642 - Word Amalgamation Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=onlinejudge&page=show_problem&problem=583 In millions of newspapers across the United States there is a word game called Jumble. The object of this game

HDOJ/HDU 1075 What Are You Talking About(字符串查找翻译~Map)

Problem Description Ignatius is so lucky that he met a Martian yesterday. But he didn't know the language the Martians use. The Martian gives him a history book of Mars and a dictionary when it leaves. Now Ignatius want to translate the history book

HDOJ/HDU 1251 统计难题(字典树啥的~Map水过)

Problem Description Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己的前缀). Input 输入数据的第一部分是一张单词表,每行一个单词,单词的长度不超过10,它们代表的是老师交给Ignatius统计的单词,一个空行代表单词表的结束.第二部分是一连串的提问,每行一个提问,每个提问都是一个字符串. 注意:本题只有一组测试数据,处理到文件结束. Output 对于每个提

HDOJ(HDU) 2192 MagicBuilding(用Java的Map做了下)

Problem Description As the increase of population, the living space for people is becoming smaller and smaller. In MagicStar the problem is much worse. Dr. Mathematica is trying to save land by clustering buildings and then we call the set of buildin

poj 1318 Word Amalgamation

这种字符串的题一定要仔细,不然很容易WA... 我开始的一种方法不是处理dictionary和sortDis,而是每次都搜索一次,到现在我都还没找到为什么错了... 后一种代码就是先处理dictionary和sortDis,之后就容易处理了 AC的代码: #include <stdio.h> #include <string.h> #include <iostream> #include <algorithm> using namespace std; ch

HDOJ/HDU 1804 Deli Deli(英语单词复数形式~)

Problem Description Mrs. Deli is running the delicatessen store "Deli Deli". Last year Mrs. Deli has decided to expand her business and build up an online store. She has hired a programmer who has implemented the online store. Recently some of h

hdu 1857 Word Puzzle

点击打开链接hdu 1857 思路:字典树 分析: 1 题目要求的是给定的单词第一个字母在这个矩形里面的最小的坐标 2 矩形的最大500*500,单词的来源有三个方向,并且单词的起点和终点在矩形之内都是可能的.所以的如果利用枚举矩形之内的单词,那么肯定是超内存的 3 所以我们必须考虑另一种的方法就是对单词进行建字典树,那么我们只要去枚举单词的可能的起点,然后进行查找相应的单词是不是在树上,如果是的话就标记一下当前的坐标. 4 注意由于单词的来源有三个方向,但是因为要求的如果下相同的情况下要求坐标

HDOJ(HDU) 4847 Wow! Such Doge!(doge字符统计)

Problem Description Chen, Adrian (November 7, 2013). "Doge Is An Ac- tually Good Internet Meme. Wow.". Gawker. Retrieved November 22, 2013. Doge is an Internet meme that became popular in 2013. The meme typically con- sists of a picture of a Shi

HDOJ/HDU 1161 Eddy&amp;#39;s mistakes(大写字母转换成小写字母)

Problem Description Eddy usually writes articles ,but he likes mixing the English letter uses, for example "computer science" is written frequently "coMpUtEr scIeNce" by him, this mistakes lets Eddy's English teacher be extremely disco