poj 1318

http://poj.org/problem?id=1318

 

这个题目还是比较水的,不过也可以提升你对字符串的熟悉度以及对一些排序函数和字符函数的使用。

大概的题意就是给你一个字典,这个字典有一些单词,先对这些单词排好序,然后再给你一串字母,看每一行字母可以组合成几个字典里的单词,可以组成的就输出

 

思路:首先把给你的单词按字母表升序进行排序,然后对每个单词进行按字母表按升序进行排序,最后对给你的每一串字母进行排序,能找到匹配的,则输出字典里的

 

 1 #include <stdio.h>
 2 #include <iostream>
 3 #include <string.h>
 4 #include <string>
 5 #include <stdlib.h>
 6 
 7 
 8 using namespace std;
 9 
10 char str_1[102][102],str_2[102][102],str_3[102][102];
11 //char str_4[102]
12 
13 int cmp(const void *a,const void *b)    //对每个字母进行排序
14 {
15     return (*(char *)a)-(*(char *)b);
16 }
17 int cmp1(const void *a,const void *b)   //对字典里的单词进行排序
18 {
19     return strcmp((char*)a,(char*)b);
20 }
21 
22 int main()
23 {
24     int first=0,second=0,flog;
25     while(gets(str_1[first]),strcmp(str_1[first],"XXXXXX"))   //遇到XXXXXX,则第一个输入结束
26             first++;
27     qsort(str_1,first,sizeof(str_2[0]),cmp1);      //对字典的单词进行排序
28     for(int i=0;i<first;i++)           //用一个字符串数组来存字典的单词,并对每一个单词的字母排序,以便于后面的比较
29     {
30         strcpy(str_2[i],str_1[i]);
31         qsort(str_2[i],strlen(str_2[i]),sizeof(str_2[i][0]),cmp);    
32     }
33     while(cin>>str_3[second],strcmp(str_3[second],"XXXXXX"))
34         {
35             qsort(str_3[second],strlen(str_3[second]),sizeof(str_3[second][0]),cmp);    
36             second++;
37         }
38     for(int j=0;j<second;j++)
39     {
40         flog=0;
41         for(int m=0;m<first;m++)
42             if(string(str_3[j])==string(str_2[m]))
43             {
44                 cout<<str_1[m]<<endl;
45                 flog++;
46             }
47         if(flog==0)
48         {
49             cout<<"NOT A VALID WORD"<<endl;
50             cout<<"******"<<endl;
51         }
52         else {
53             cout<<"******"<<endl;
54         }
55     }
56     return 0;
57 }

 

转载于:https://www.cnblogs.com/Tree-dream/p/5364915.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值