zoukankan      html  css  js  c++  java
  • hdu

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

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

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

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cmath>
     4 #include <vector>
     5 #include <cstring>
     6 #include <string>
     7 #include <algorithm>
     8 #include <string>
     9 #include <set>
    10 #include <functional>
    11 #include <numeric>
    12 #include <sstream>
    13 #include <stack>
    14 #include <map>
    15 #include <queue>
    16 #pragma comment(linker, "/STACK:102400000,102400000")
    17 #define CL(arr, val)    memset(arr, val, sizeof(arr))
    18 
    19 #define ll long long
    20 #define inf 0x7f7f7f7f
    21 #define lc l,m,rt<<1
    22 #define rc m + 1,r,rt<<1|1
    23 #define pi acos(-1.0)
    24 
    25 #define L(x)    (x) << 1
    26 #define R(x)    (x) << 1 | 1
    27 #define MID(l, r)   (l + r) >> 1
    28 #define Min(x, y)   (x) < (y) ? (x) : (y)
    29 #define Max(x, y)   (x) < (y) ? (y) : (x)
    30 #define E(x)        (1 << (x))
    31 #define iabs(x)     (x) < 0 ? -(x) : (x)
    32 #define OUT(x)  printf("%I64d
    ", x)
    33 #define lowbit(x)   (x)&(-x)
    34 #define Read()  freopen("a.txt", "r", stdin)
    35 #define Write() freopen("b.txt", "w", stdout);
    36 #define maxn 1000000000
    37 #define N 2510
    38 #define mod 1000000000
    39 using namespace std;
    40 
    41 struct node
    42 {
    43     string s[105];
    44     int x;
    45 };
    46 node s1,s2,s3;
    47 int main()
    48 {
    49     //freopen("a.txt","r",stdin);
    50     int n=0;
    51     string str;
    52     while(cin>>str)
    53     {
    54         if(str=="XXXXXX")break;
    55         s1.s[n++]=str;
    56     }
    57     //cout<<n<<endl;
    58     for(int i=0;i<n;i++)
    59     {
    60         s2.s[i]=s1.s[i];
    61         sort(s1.s[i].begin(),s1.s[i].end());
    62     }
    63     while(cin>>str)
    64     {
    65         if(str=="XXXXXX") break;
    66         sort(str.begin(),str.end());
    67         bool flag=0;
    68         int k=0;
    69         for(int j=0;j<n;j++)
    70             if(str==s1.s[j])
    71             {
    72                 flag=1;
    73                 s3.s[k++]=s2.s[j];
    74             }
    75         for(int i=0;i<k;i++)
    76             for(int j=i+1;j<k;j++)
    77             {
    78                 if(s3.s[i]>s3.s[j])
    79                 {
    80                     str=s3.s[i];
    81                     s3.s[i]=s3.s[j];
    82                     s3.s[j]=str;
    83                 }
    84             }
    85         for(int i=0;i<k;i++)
    86             cout<<s3.s[i]<<endl;
    87         if(!flag) cout<<"NOT A VALID WORD"<<endl;
    88         cout<<"******"<<endl;
    89     }
    90     return 0;
    91 }

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

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cmath>
     4 #include <vector>
     5 #include <cstring>
     6 #include <string>
     7 #include <algorithm>
     8 #include <string>
     9 #include <set>
    10 #include <functional>
    11 #include <numeric>
    12 #include <sstream>
    13 #include <stack>
    14 #include <map>
    15 #include <queue>
    16 #pragma comment(linker, "/STACK:102400000,102400000")
    17 #define CL(arr, val)    memset(arr, val, sizeof(arr))
    18 
    19 #define ll long long
    20 #define inf 0x7f7f7f7f
    21 #define lc l,m,rt<<1
    22 #define rc m + 1,r,rt<<1|1
    23 #define pi acos(-1.0)
    24 
    25 #define L(x)    (x) << 1
    26 #define R(x)    (x) << 1 | 1
    27 #define MID(l, r)   (l + r) >> 1
    28 #define Min(x, y)   (x) < (y) ? (x) : (y)
    29 #define Max(x, y)   (x) < (y) ? (y) : (x)
    30 #define E(x)        (1 << (x))
    31 #define iabs(x)     (x) < 0 ? -(x) : (x)
    32 #define OUT(x)  printf("%I64d
    ", x)
    33 #define lowbit(x)   (x)&(-x)
    34 #define Read()  freopen("a.txt", "r", stdin)
    35 #define Write() freopen("b.txt", "w", stdout);
    36 #define maxn 1000000000
    37 #define N 2510
    38 #define mod 1000000000
    39 using namespace std;
    40 
    41 struct node
    42 {
    43     char s1[105],s2[105];
    44     int id;
    45     bool operator < (const node a) const
    46     {
    47         return strcmp(s1,a.s1)<0;
    48     }
    49 }p[105];
    50 
    51 int main()
    52 {
    53    // freopen("a.txt","r",stdin);
    54     char str[105];
    55     int n=0;
    56     while(~scanf("%s",p[n].s1))
    57     {
    58         if(strcmp(p[n].s1,"XXXXXX")==0) break;
    59         strcpy(p[n].s2,p[n].s1);
    60         n++;
    61     }
    62     sort(p,p+n);  //先对  所有字符串按照字典序排序 
    63     for(int i=0;i<n;i++)
    64     {
    65         int l=strlen(p[i].s1);
    66         sort(p[i].s1,p[i].s1+l);  //再对  每个字符串排序
    67     }
    68     while(~scanf("%s",str))
    69     {
    70         if(strcmp(str,"XXXXXX")==0) break;
    71         int l=strlen(str);
    72         sort(str,str+l);
    73         bool flag=0;
    74         for(int j=0;j<n;j++)
    75             if(strcmp(p[j].s1,str)==0)
    76             {
    77                 flag=1;
    78                 printf("%s
    ",p[j].s2);
    79             }
    80         if(!flag) printf("NOT A VALID WORD
    ");
    81         printf("******
    ");
    82     }
    83     return 0;
    84 }
  • 相关阅读:
    webpack4.0--安装操作
    刮刮卡制作思路--canvas
    Webpack 基础了解
    音量控制条demo,拖拽定位,点击定位
    我收到过的最好的职场建议——Nicholas C. Zakas
    跨域通信--Window.postMessage()
    小球沿贝塞尔二阶曲线的运动
    ES6 简写方式
    React 基础编写
    数据结构(十三)— 树结构之二叉树
  • 原文地址:https://www.cnblogs.com/nowandforever/p/4551263.html
Copyright © 2011-2022 走看看