题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1113

题意:输入一个字典,然后再输入若干单词(每行中,1 <= 单词数 <= 100,并且每个单词在字典中保证是唯一的),用XXXXXX结尾来表示字典的结束。接着输入一个单词word(1 <= 字母个数 <= 6),每个单词你都需要在字典中找出所有可以用word的字母重排后得到的单词,并按照字典序从小到大的顺序在一行中输出;如果不存在,则输出“NOT A VALID WORD”。注意:字典中的单词不一定要按字典序顺序排列,但可以用word的字母重排后得到的单词必须要按字典序排列。

这里参考了《算法竞赛入门经典》中的例题:字母重排 来做的,自以为看懂后能快速地写出来,但是发现好几个bug。这就是实践的重要性啊,不过还是很欣喜地通过这题学会了qsort函数(要包含头文件stdlib.h)的用法(

http://baike.baidu.com/view/982231.htm)还有关于int cmp_string(const void *a,const void *b) 的理解(

http://hi.baidu.com/lyb1900/item/4698682bd85389fa51fd875c

三个qsort处理后的结果如下(以样例数据为例):

样例数据:

tarp  given  score  refund  only  trap  work  earn  course  pepper  part

第一个:  qsort(dic, n, sizeof(dic[0]), cmp_string);

course  earn  given  only  part  pepper  refund  score  tarp  trap  work

第二个:    qsort(sorted[i], strlen(sorted[i]), sizeof(char), cmp_char);

ceorsu  aenr  eginv  lnoy  aprt  eepppr  defnru  ceors  aprt  aprt  korw

第三个: qsort(word, strlen(word), sizeof(char), cmp_char);

就是把输入的单词按字典序排序,比如输入nfudre,它就会变成defnru,然后比对回dic相应的位置,就能查出是refund这个单词了。

 #include <iostream>
#include <string.h>
#include <stdlib.h>
using namespace std; char dic[][];
char sorted[][];
char word[]; // 字符比较函数
int cmp_char(const void *_a, const void *_b)
{
char *a = (char *)_a;
char *b = (char *)_b;
return *a - *b;
} // 字符串比较函数
int cmp_string(const void *_a, const void *_b)
{
char *a = (char *)_a;
char *b = (char *)_b;
return strcmp(a, b);
} int main()
{
int i, n = ;
while ()
{
scanf("%s", dic[n]);
if (dic[n][] == 'X') // 遇到结束标志就终止循环
break;
n++;
}
qsort(dic, n, sizeof(dic[]), cmp_string); // 给字典中所有单词排序(单词与单词之间)
/*
for (i = 0; i < n; i++)
{
cout << dic[i] << "\t";
}
cout << endl;
*/
for (i = ; i < n; i++)
{
// cout << dic[i] << '\t';
strcpy(sorted[i], dic[i]); // 字符串复制,注意不能直接用dic进行每个单词排序,否则会找不到字典中原有的单词
qsort(sorted[i], strlen(sorted[i]), sizeof(char), cmp_char); // 给每个单词排序(单词内部)
// cout << sorted[i] << endl;
}
while (scanf("%s", word))
{
int flag = ;
if (word[] == 'X')
break;
qsort(word, strlen(word), sizeof(char), cmp_char); // 给输入单词排序
// cout << word << endl;
for (i = ; i < n; i++)
{
if (strcmp(sorted[i], word) == )
{
printf("%s\n", dic[i]); // 输出原始单词,而不是排序后的
flag = ;
}
}
if (!flag)
printf("NOT A VALID WORD\n");
printf("******\n");
}
return ;
}

hdu 1113 Word Amalgamation 解题报告的更多相关文章

  1. hdu 1113 Word Amalgamation

    原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1113 字符串简单题: stl水过 如下: #include<algorithm> #inc ...

  2. hdu - 1113 Word Amalgamation (stl)

    http://acm.hdu.edu.cn/showproblem.php?pid=1113 给定一个字典,然后每次输入一个字符串问字典中是否有单词与给定的字符串的所有字母一样(顺序可以打乱),按字典 ...

  3. HDU 1113 Word Amalgamation (map 容器 + string容器)

    http://acm.hdu.edu.cn/showproblem.php?pid=1113 Problem Description In millions of newspapers across ...

  4. HDOJ/HDU 1113 Word Amalgamation(字典顺序~Map)

    Problem Description In millions of newspapers across the United States there is a word game called J ...

  5. HDU 4303 Hourai Jeweled 解题报告

    HDU 4303 Hourai Jeweled 解题报告 评测地址: http://acm.hdu.edu.cn/showproblem.php?pid=4303 评测地址: https://xoj. ...

  6. 【九度OJ】题目1113:二叉树 解题报告

    [九度OJ]题目1113:二叉树 解题报告 标签(空格分隔): 九度OJ http://ac.jobdu.com/problem.php?pid=1113 题目描述: 如上所示,由正整数1,2,3-- ...

  7. HDOJ.1113 Word Amalgamation(map)

    Word Amalgamation 点我挑战题目 点我一起学习STL-MAP 题意分析 给出字典.之后给出一系列======乱序======单词,要求你查字典,如过这个乱序单词对用有多个有序单词可以输 ...

  8. hdu 2544 最短路 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2544 题目意思:给出 n 个路口和 m 条路,每一条路需要 c 分钟走过.问从路口 1 到路口 n 需 ...

  9. 【LeetCode】408. Valid Word Abbreviation 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 日期 题目地址:https://leetcod ...

随机推荐

  1. 【深入】java 单例模式(转)

    [深入]java 单例模式 关于单例模式的文章,其实网上早就已经泛滥了.但一个小小的单例,里面却是有着许多的变化.网上的文章大多也是提到了其中的一个或几个点,很少有比较全面且脉络清晰的文章,于是,我便 ...

  2. POJ1995 Raising Modulo Numbers

    Raising Modulo Numbers Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 6373   Accepted: ...

  3. PL/0编译器(java version) - Interpreter.java

    1: package compiler; 2:   3: import java.io.BufferedReader; 4: import java.io.BufferedWriter; 5: imp ...

  4. curl 命令行应用

    我一向以为,curl只是一个编程用的函数库. 最近才发现,这个命令本身,就是一个无比有用的网站开发工具,请看我整理的它的用法. =================================== ...

  5. ubuntu下安装 openssl 开发库

    ubuntu下安装 openssl 开发库 检查是否已安装openssl: sudo apt-get install openssl 如果已安装执行以下操作:sudo apt-get install ...

  6. 极大似然估计、贝叶斯估计、EM算法

    参考文献:http://blog.csdn.net/zouxy09/article/details/8537620 极大似然估计 已知样本满足某种概率分布,但是其中具体的参数不清楚,极大似然估计估计就 ...

  7. JS小记

    好记性不如烂笔头. 1.document.ElementFromPoint:根据坐标获得元素 2.有时候要操作DOM页面,但是得不到预期结果,很可能是因为页面还没加载完成,在console控制台可以看 ...

  8. JS实现打字机式字符输出效果

    <html> <head> <meta http-equiv="Content-Type" content="text/html; char ...

  9. UIGestureRecognizer ios手势识别温习

    1.UIGestureRecognizer介绍 手势识别在iOS上非常重要,手势操作移动设备的重要特征,极大的增加了移动设备使用便捷性. iOS系统在3.2以后,为方便开发这使用一些常用的手势,提供了 ...

  10. PHP array_intersect() 函数

    PHP Array 函数 定义和用法 array_intersect() 函数返回两个或多个数组的交集数组. 结果数组包含了所有在被比较数组中,也同时出现在所有其他参数数组中的值,键名保留不变. 注释 ...