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. 在spring boot 中使用itext和itextrender生成pdf文件

    转载请注明出处 https://www.cnblogs.com/majianming/p/9539376.html 项目中需要对订单生成pdf文件,在第一版本其实已经有了比较满意的pdf文档,但是还是 ...

  2. ASP.NET MVC数据库初始化

    public class DBInitializer:DropCreateDatabaseAlways<BookDBContext> { protected override void S ...

  3. POI导出时,将指定的列设置为下拉列表

    本示例设置第2列为下拉框(下拉框内容为:是/否),从第5行开始到5657行结束. 关键代码示例: ComboxList = new String[]{"是","否&quo ...

  4. 5.1点击4个按钮显示相应的div

    事件:onclick 属性:display,className 用到for语句,index标记,this当前事件 先清空后附加 <!DOCTYPE html><html>< ...

  5. 解决windows下rstudio安装playwith包报错问题

    一.playwith包简介 playwith包提供了一个GTK+图形用户界面(GUI),使得用户可以编辑R图形并与其交互.playwith()函数允许用户识别和标注点.查看一个观测所有的变量值.缩放和 ...

  6. Farseer.net轻量级ORM开源框架 V1.2版本升级消息

    V1.1到V1.2的更新,重构了很多类及方法,其中主要做了性能优化(取消所有反射,使用表达式树+缓存).解耦了SQL生成层(没有实体.队列的依赖,所有数据均通过表达式树传递解析) 先上内部更新历史记录 ...

  7. spring3 上配置quartz 任务调度

    maven配置: <dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>qu ...

  8. ubuntu 下安装redis

    获取Redis 1.通过官网http://redis.io/获取稳定版源码包下载地址: 2.通过wget http://download.redis.io/releases/redis-3.0.2.t ...

  9. pythno学习小结-替换python字典中的key值

    源: d={'a':1,'b':2,'c':3} 目标:key:'b'替换为'e' d={'a':1,'e':2,'c':3} 方法: d['e']=d.pop('b')

  10. Android(java)学习笔记201:JNI之helloword案例(利用NDK工具)

    1. 逻辑思路过程图: 2.下面通过一个HelloWorld案例来说明一下JNI利用NDK开发过程(步骤) 分析:我们在Win7系统下编译的C语言代码,我们知道C语言依赖操作系统,不能跨平台,所以我们 ...