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. 工作记录 angular页面操作 MD5加密

    今天只是做页面,基于angularjs,有美工做的图打底,确实好用 密码保存,用到了C# MD5加密: https://www.cnblogs.com/healer007/p/5062189.html

  2. Javascript数据结构之栈

    作者原文:http://hawkzz.com/blog/blog/1515054561771 定义 栈是一种特殊的列表,栈内的元素只能通过列表的一端访问,这一端称为栈顶.栈被称为一种先入后出的数据结构 ...

  3. Toast解析

    课程Demo public class MainActivity extends AppCompatActivity { private Button bt1; private Button bt2; ...

  4. 阿里云设置指定ip访问实例

    添加安全组规则 添加允许访问的外网IP,优先级设置为1,并将所有ip设置为拒绝访问,优先级设置为2. 参考地址: https://help.aliyun.com/document_detail/254 ...

  5. C# 移动开发 MasterDetailPage 侧滑

    先上结果图: 虽然是跨平台的安卓和ios都可以运行,由于目前只配置了安卓的,ios的先不理. 我们先新建一个项目,跨平台应用: 可移植类库: 可移植项目右键添加新建项 选 Forms MasterDe ...

  6. Node.js——body方式提交数据

    引入核心模块 http,利用其 api(http.createServer) 返回一个 http.server 实例,这个实例是继承于net.Server,net.Server 也是通过net.cre ...

  7. leetcode_Counting Bits_dp

    给定num,用O(num)的时间复杂度计算0--num中所有数的二进制表示中1的个数. vector<int> countBits(int num) { vector<,); ;i& ...

  8. HDU_1710_二叉树前序中序确定后序

    2018-3-6 按照王道机试书上的思路再做了一遍,先根据先序和中序建树,然后后序遍历. 静态分配数组用于建树,可以返回数组地址当作结点指针. #include<iostream> #in ...

  9. CREATE SEQUENCE - 创建一个新的序列发生器

    SYNOPSIS CREATE [ TEMPORARY | TEMP ] SEQUENCE name [ INCREMENT [ BY ] increment ] [ MINVALUE minvalu ...

  10. faster rcnn结构

    rpn-data层输入的是data即整张图片,然后是根据映射生成roi框 rpn-loss-bbox输入的才是整个网络预测的roi框 bbox_transform在rpn-data层使用,把生成的ac ...