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 ...
随机推荐
- BZOJ-1491 社交网络 FLoyd+乱搞
感觉这两天一直在做乱搞的题... 1491: [NOI2007]社交网络 Time Limit: 10 Sec Memory Limit: 64 MB Submit: 1279 Solved: 732 ...
- Session解析
1.除非关闭所有页面 或者超时session才销毁 2.在几个页面之间切换的时候 session保存用户状态. 3.遍历数组时候for循环中从0开始小于长度,不等于长度,用Matlab用习惯了,竟然从 ...
- iOS推送失败的可能问题汇总
ITC上的证书问题 AppID未开启推送 Provioning Profile在AppID开启推送功能前生成的 Provioning证书过期 推送的pem证书过期 客户端问题 target的CodeS ...
- OAuth2.0认证和授权原理
什么是OAuth授权? 一.什么是OAuth协议 OAuth(开放授权)是一个开放标准. 允许第三方网站在用户授权的前提下访问在用户在服务商那里存储的各种信息. 而这种授权无需将用户提供用户名和密 ...
- ci默认控制器
默认控制器 前面提到,如果在请求中没有指明具体的控制器,CI将会把页面重定向到一个系统默认的页面.这个默认页面可以自己设定,它存放在如下地址:/system/application/config/ro ...
- WPF 屏蔽Alt+F4强制退出
if (e.KeyStates == Keyboard.GetKeyStates(Key.F4) && Keyboard.Modifiers == ModifierKeys.Alt) ...
- hdu.5203.Rikka with wood sticks(数学推导:一条长度为L的线段经分割后可以构成几种三角形)
Rikka with wood sticks Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/O ...
- Spring+Quartz实现定时任务的配置方法
1.Scheduler的配置 <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean" ...
- android webview删除缓存
[1].[代码] 删除保存于手机上的缓存. 跳至 [1] [2] [3] 01 // clear the cache before time numDays 02 private int cl ...
- [BZOJ2959]长跑——新技能:LCT+缩圈
[BZOJ2959]长跑 试题描述 某校开展了同学们喜闻乐见的阳光长跑活动.为了能“为祖国健康工作五十年”,同学们纷纷离开寝室,离开教室,离开实验室,到操场参加3000米长跑运动.一时间操场上熙熙攘攘 ...