swustoj变位词(0549)

来源:互联网 发布:大数据与市场调研 编辑:程序博客网 时间:2024/05/22 13:40

输入N和一个要查找的字符串,以下有N个字符串,我们需要找出其中的所有待查找字符串的变位词(例如eat,eta,aet就是变位词)按字典序列输出,并且输出总数目

Description

第一行:N(代表共有N个字符串属于被查找字符串) (N<=50) 第二行:待查找的字符串(不大于10个字符) 以下N行:被查找字符串(不大于10个字符)

Input

按字典序列输出在被查找字符串中待查找字符串的所有变位词 每行输出一个 输出完成后输出总数目

Output
1
2
3
4
5
6
7
8
9
7
asdfg
asdgf
asdfg
dsafg
xcvcv
gfdsa
tyuv
asd
Sample Input
1
2
3
4
5
asdfg
asdgf
dsafg
gfdsa
4
Sample Output
/*水题:直接暴力可以过*/#include <stdio.h>#include <string.h>#include<iostream>#include<stack>#include<algorithm>using namespace std;struct node{char str[15];};node a[55], b[55];bool cmp1(char a, char b){return a < b;}bool cmp2(node xx, node yy){if (strcmp(xx.str, yy.str) < 0)return 1;elsereturn 0;}int main(){int n;char s[15];while (cin >> n){//getchar();cin >> s;sort(s, s + strlen(s), cmp1);//cout << s << endl;for (int i = 0; i < n; i++){scanf("%s", a[i].str);}sort(a, a + n, cmp2);int k = 0;int ans = 0;for (int i = 0; i < n; i++){node temp;temp = a[i];sort(temp.str, temp.str + strlen(temp.str), cmp1);if (strcmp(temp.str, s) == 0){b[k++] = a[i];ans++;}}for (int i = 0; i < k; i++){cout << b[i].str << endl;}cout << ans << endl;}return 0;}

0 1