hdu 1113 Word Amalgamation 解题报告
题目链接: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 解题报告的更多相关文章
- 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 (map 容器 + string容器)
http://acm.hdu.edu.cn/showproblem.php?pid=1113 Problem Description In millions of newspapers across ...
- HDOJ/HDU 1113 Word Amalgamation(字典顺序~Map)
Problem Description In millions of newspapers across the United States there is a word game called J ...
- HDU 4303 Hourai Jeweled 解题报告
HDU 4303 Hourai Jeweled 解题报告 评测地址: http://acm.hdu.edu.cn/showproblem.php?pid=4303 评测地址: https://xoj. ...
- 【九度OJ】题目1113:二叉树 解题报告
[九度OJ]题目1113:二叉树 解题报告 标签(空格分隔): 九度OJ http://ac.jobdu.com/problem.php?pid=1113 题目描述: 如上所示,由正整数1,2,3-- ...
- HDOJ.1113 Word Amalgamation(map)
Word Amalgamation 点我挑战题目 点我一起学习STL-MAP 题意分析 给出字典.之后给出一系列======乱序======单词,要求你查字典,如过这个乱序单词对用有多个有序单词可以输 ...
- hdu 2544 最短路 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2544 题目意思:给出 n 个路口和 m 条路,每一条路需要 c 分钟走过.问从路口 1 到路口 n 需 ...
- 【LeetCode】408. Valid Word Abbreviation 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 日期 题目地址:https://leetcod ...
随机推荐
- BIEE连接数据库的方法
BI创建(数据)分析.仪表盘.报表前,都需要对数据进行建模,在oracle biee里称为创建“资料档案库”-该文件后缀为RPD,所以一般也称为创建RPD文件. 步骤: 1.从windows开始菜单里 ...
- 使用multi curl进行http并发访问
curl是一款利用URL语法进行文件传输的工具,它支持多种协议,包括FTP, FTPS, HTTP, HTTPS, GOPHER, TELNET等,我们既可以在命令行上使用它,也可以利用 libcur ...
- Selenium2+python自动化13-Alert
不是所有的弹出框都叫alert,在使用alert方法前,先要识别出它到底是不是alert.先认清楚alert长什么样子,下次碰到了,就可以用对应方法解决.alert\confirm\prompt弹出框 ...
- MySQL 中 where id in (1,2,3,4,...) 的效率问题讨论
MySQL ACMAIN_CHM06-26 16:36 等级 84次回复 [求证&散分]MySQL 中 where id in (1,2,3,4,...) 的效率问题讨论 庆祝本月大版得 ...
- jquery------提供灵活的方法参数
index.jsp <h1 >再次重逢的世界</h1> my.js $(document).ready(function(){ (function($){ $.fn.shado ...
- SQL Server中追踪器Trace的介绍和简单使用
一.What is Trace? 对于SQL Profiler这个工具相信大家都不是很陌生,没用过的朋友可以在SQL Server Management Studio>工具>SQL Ser ...
- MySQL查询重复出现次数最多的记录
MySQL查询的方法很多,下面为您介绍的MySQL查询语句用于实现查询重复出现次数最多的记录,对于学习MySQL查询有很好的帮助作用. 在有些应用里面,我们需要查询重复次数最多的一些记录,虽然这是一个 ...
- String与Date、Timestamp互转
一.String与Date(java.util.Date)互转 1.1 String -> Date String dateStr = "2010/05/04 12:34:23&quo ...
- Jetty和tomcat的比较
转自: http://blog.csdn.net/classicbear/article/details/6968930 相同点: 1. Tomcat和Jetty都是一种Servlet引擎, ...
- TreeSet里面放对象,如果同时放入了父类和子类的实例对象,那比较时使用的是父类的compareTo方法,还是使用的子类的compareTo方法,还是抛异常!
/** * * @author ocq */ class Parent implements Comparable { private int age = 0; public Parent(int a ...