swustoj(变位词(0549))

来源:互联网 发布:手机淘宝几天到货 编辑:程序博客网 时间:2024/06/05 18:08

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

Description

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

Input

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

Output

7
asdfg
asdgf
asdfg
dsafg
xcvcv
gfdsa
tyuv
asd
Sample Input

asdfg
asdgf
dsafg
gfdsa
4
#include<iostream>#include<string.h>#include<algorithm>using namespace std;struct node{char ch[15];}a[55],b[55];bool cmp(node x, node y){if (strcmp(x.ch, y.ch) > 0)return 0;return 1;}bool cmp1(char x, char y){return x < y;}int main(){int n;while (cin >> n){char ch[15];cin >> ch;int lc = strlen(ch);char t1[15];char t2[15];strcpy(t1, ch);sort(t1, t1 + lc, cmp1);int k=0;for (int i = 0; i < n; i++){cin >> a[i].ch;if (strlen(a[i].ch) == lc){strcpy(t2, a[i].ch);sort(t2, t2 + lc, cmp1);if (strcmp(t1,t2)==0){b[k++] = a[i];}}}sort(b, b + k, cmp);for (int i = 0; i < k; i++)cout << b[i].ch << endl;cout << k << endl;}}
这是网上找的一篇代码,自己写的不知道错哪了,一直wrong,,,
1 1