http://acm.hdu.edu.cn/showproblem.php?pid=1113

给定一个字典,然后每次输入一个字符串问字典中是否有单词与给定的字符串的所有字母一样(顺序可以打乱),按字典序输出字典中的原字符串。

我开始是直接用了 sort, 用一个结构体记录了所有字符串,和相应下标,输出的时候在用了冒泡排序。

 #include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <cstring>
#include <string>
#include <algorithm>
#include <string>
#include <set>
#include <functional>
#include <numeric>
#include <sstream>
#include <stack>
#include <map>
#include <queue>
#pragma comment(linker, "/STACK:102400000,102400000")
#define CL(arr, val) memset(arr, val, sizeof(arr)) #define ll long long
#define inf 0x7f7f7f7f
#define lc l,m,rt<<1
#define rc m + 1,r,rt<<1|1
#define pi acos(-1.0) #define L(x) (x) << 1
#define R(x) (x) << 1 | 1
#define MID(l, r) (l + r) >> 1
#define Min(x, y) (x) < (y) ? (x) : (y)
#define Max(x, y) (x) < (y) ? (y) : (x)
#define E(x) (1 << (x))
#define iabs(x) (x) < 0 ? -(x) : (x)
#define OUT(x) printf("%I64d\n", x)
#define lowbit(x) (x)&(-x)
#define Read() freopen("a.txt", "r", stdin)
#define Write() freopen("b.txt", "w", stdout);
#define maxn 1000000000
#define N 2510
#define mod 1000000000
using namespace std; struct node
{
string s[];
int x;
};
node s1,s2,s3;
int main()
{
//freopen("a.txt","r",stdin);
int n=;
string str;
while(cin>>str)
{
if(str=="XXXXXX")break;
s1.s[n++]=str;
}
//cout<<n<<endl;
for(int i=;i<n;i++)
{
s2.s[i]=s1.s[i];
sort(s1.s[i].begin(),s1.s[i].end());
}
while(cin>>str)
{
if(str=="XXXXXX") break;
sort(str.begin(),str.end());
bool flag=;
int k=;
for(int j=;j<n;j++)
if(str==s1.s[j])
{
flag=;
s3.s[k++]=s2.s[j];
}
for(int i=;i<k;i++)
for(int j=i+;j<k;j++)
{
if(s3.s[i]>s3.s[j])
{
str=s3.s[i];
s3.s[i]=s3.s[j];
s3.s[j]=str;
}
}
for(int i=;i<k;i++)
cout<<s3.s[i]<<endl;
if(!flag) cout<<"NOT A VALID WORD"<<endl;
cout<<"******"<<endl;
}
return ;
}

也可以直接sort排序,定义一个结构体,保存原字符串和排序后的字符串,先对所有字符串按照字典序排,这样先输出的就是字典序小的。

 #include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <cstring>
#include <string>
#include <algorithm>
#include <string>
#include <set>
#include <functional>
#include <numeric>
#include <sstream>
#include <stack>
#include <map>
#include <queue>
#pragma comment(linker, "/STACK:102400000,102400000")
#define CL(arr, val) memset(arr, val, sizeof(arr)) #define ll long long
#define inf 0x7f7f7f7f
#define lc l,m,rt<<1
#define rc m + 1,r,rt<<1|1
#define pi acos(-1.0) #define L(x) (x) << 1
#define R(x) (x) << 1 | 1
#define MID(l, r) (l + r) >> 1
#define Min(x, y) (x) < (y) ? (x) : (y)
#define Max(x, y) (x) < (y) ? (y) : (x)
#define E(x) (1 << (x))
#define iabs(x) (x) < 0 ? -(x) : (x)
#define OUT(x) printf("%I64d\n", x)
#define lowbit(x) (x)&(-x)
#define Read() freopen("a.txt", "r", stdin)
#define Write() freopen("b.txt", "w", stdout);
#define maxn 1000000000
#define N 2510
#define mod 1000000000
using namespace std; struct node
{
char s1[],s2[];
int id;
bool operator < (const node a) const
{
return strcmp(s1,a.s1)<;
}
}p[]; int main()
{
// freopen("a.txt","r",stdin);
char str[];
int n=;
while(~scanf("%s",p[n].s1))
{
if(strcmp(p[n].s1,"XXXXXX")==) break;
strcpy(p[n].s2,p[n].s1);
n++;
}
sort(p,p+n); //先对 所有字符串按照字典序排序
for(int i=;i<n;i++)
{
int l=strlen(p[i].s1);
sort(p[i].s1,p[i].s1+l); //再对 每个字符串排序
}
while(~scanf("%s",str))
{
if(strcmp(str,"XXXXXX")==) break;
int l=strlen(str);
sort(str,str+l);
bool flag=;
for(int j=;j<n;j++)
if(strcmp(p[j].s1,str)==)
{
flag=;
printf("%s\n",p[j].s2);
}
if(!flag) printf("NOT A VALID WORD\n");
printf("******\n");
}
return ;
}

hdu - 1113 Word Amalgamation (stl)的更多相关文章

  1. hdu 1113 Word Amalgamation

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

  2. hdu 1113 Word Amalgamation 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1113 题意:输入一个字典,然后再输入若干单词(每行中,1 <= 单词数 <= 100,并且 ...

  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. HDOJ.1113 Word Amalgamation(map)

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

  6. hdu1113 Word Amalgamation(详解--map和string的运用)

    版权声明:本文为博主原创文章.未经博主同意不得转载. vasttian https://blog.csdn.net/u012860063/article/details/35338617 转载请注明出 ...

  7. Word Amalgamation(枚举 + 排序)

    Word Amalgamation Time Limit: 1 Sec  Memory Limit: 64 MB Submit: 373  Solved: 247 Description In mil ...

  8. hdu-----(1113)Word Amalgamation(字符串排序)

    Word Amalgamation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  9. Word Amalgamation(hdoj1113)

    Word Amalgamation Problem Description In millions of newspapers across the United States there is a ...

随机推荐

  1. JAVA300集笔记

    章节2 java入门阶段 2.1注释 单行注释 //  多行注释 /* 内容*/ 文本注释/**内容*/ 注释是为了方便阅读代码,在编译时注释会被删除. 2.2 标识符 标识符作用: 标识符用来给变量 ...

  2. div里面整齐的字体样式,所有浏览器都兼容

    <div id="wenda"> <div class="table_wd" > <div class="tr1&quo ...

  3. robotframework + python2.7.9 + selenium 2.44.0 + selenium2library1.7 测试环境搭建成功!

    真心不容易呀!开源软件搭建挺麻烦的,各种组件未必要使用最新的版本:有些最新版本反而不兼容.需要仔细看官方说明书来进行搭建(官方网站都是英文),所以闹得重新安装了几次. 先上测试用例通过的图:

  4. 掌握Spark机器学习库(课程目录)

    第1章 初识机器学习 在本章中将带领大家概要了解什么是机器学习.机器学习在当前有哪些典型应用.机器学习的核心思想.常用的框架有哪些,该如何进行选型等相关问题. 1-1 导学 1-2 机器学习概述 1- ...

  5. C#面试问题及答案

    1.遇到高并发的问题如何解决? 优化SQL语句 多线程 分布式服务器 集群 拆表2.Dictionary和ConurrentDictionary的区别? 后者是线程安全的 前者适用于单线程3.Dict ...

  6. python Matplotlib 系列教程(三)——绘制直方图和条形图

    在本章节我们将学习如何绘制条形图和直方图 条形图与直方图的区别:首先,条形图是用条形的长度表示各类别频数的多少,其宽度(表示类别)则是固定的: 直方图是用面积表示各组频数的多少,矩形的高度表示每一组的 ...

  7. 使用webpack搭建react项目 webpack-react-project

    webpack-react-project 使用webpack搭建react项目 webpack搭建react项目 github源码 具体配置信息参照package.json和webpack.conf ...

  8. HDU - 2102 A计划(双层BFS)

    题目: 可怜的公主在一次次被魔王掳走一次次被骑士们救回来之后,而今,不幸的她再一次面临生命的考验.魔王已经发出消息说将在T时刻吃掉公主,因为他听信谣言说吃公主的肉也能长生不老.年迈的国王正是心急如焚, ...

  9. Samba 学习笔记

    这个网站不错.https://www.ibm.com/developerworks/cn/linux/l-lpic3-311-1/

  10. 树莓派 - RPi.GPIO

    RPi.GPIO是通过Python/C API实现的,C代码操作底层寄存器, python通过Python/C API调用这些C接口. 这是关于RPi.GPIO项目的介绍. 其中提到了有python ...