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. [转]windows azure How to use Blob storage from .NET

    本文转自:http://azure.microsoft.com/en-us/documentation/articles/storage-dotnet-how-to-use-blobs/?rnd=1 ...

  2. 为OS X开发者准备的15个超棒应用

    几乎所有的开发人员在他们日常的开发工作中都有他们自己不可缺少的工具或实用程序集. 这些工具中的每一个都提供了特定的功能,大多数开发者都已经将他们集成到了其工作流程中. 使用这些工具或实用程序不单单只是 ...

  3. Python 设计模式--简单工厂模式

    简单工厂模式(Factory Pattern)是一种创建型的设计模式,像工厂一样根据要求生产对象实例. 特点:根据不同的条件,工厂实例化出合适的对象. <大话设计模式>中实例:四则运算计算 ...

  4. No rule to make target ...

    在编译一个Android上的jni的时候出现了如下的问题 make[3]: *** No rule to make target `/home/zhang/android1/src/androidpk ...

  5. 【PostgreSQL-9.6.3】进程及体系结构

    本文主要讲述了PG的几个主要进程,以及PG的核心架构.进程和体系结构详见下图: 从上面的体系结构图可以看出来,PG使用经典的C/S架构,进程架构.在服务器端有主进程.服务进程.子进程.共享内存以及文件 ...

  6. OpenMP入门教程(二)

    OpenMP API概述 OpenMP由三部分组成: 编译指令(19) 运行时库程序(32) 环境变量(9) 后来的API包含同样的三个组件,只是三者的数量都有所增加. 编译器指令 OpenMP编译器 ...

  7. 关闭 将jar或者aar发布到到mvn 中(用github作为仓库), 通过gradle dependency 方式集成

    使用Android Studio开发的用户,都希望通过maven远程仓库的方式来集成jar.aar文件,但是这些文件时如何发布的呢? 通常开发者都会将jar文件发布到sonatype上,以提供给其他开 ...

  8. Oracle learning note

    oracle SQL select 'para1' || 'para2' as "para" must "" from table t where c.name ...

  9. Java中this、static关键字的内存图解

    Java中的关键字有很多,abstract  default  goto*  null  switch  boolean  do  if  package  nchronzed  break  dou ...

  10. IDEA打war包部署本地TOMCAT测试

    在Eclipse中打war包很方便,导出即可直接选择war包,如下图所示: 但是在IDEA中有点麻烦,网上很多教程,做的也都很好,我自己也做一份,加深一下印象. 首先打开File->Projec ...