UVA - 156 Ananagrams
2016-07-09 18:29
471 查看
题目大意:输入一些单词,按字典序顺序输出无视大小写及字母顺序后,只出现一次的单词。如 STOP、SOPT、spTO 算重复出现。
解题思路:保留输入单词,把输入的单词化为小写后对字符串排序,这时候出现字母个数相同的单词会被排成一样,然后遍历查找是否有相同单词,没有的话把对应原单词存于一个二维数组中,对二维数组排序后输出。
解题思路:保留输入单词,把输入的单词化为小写后对字符串排序,这时候出现字母个数相同的单词会被排成一样,然后遍历查找是否有相同单词,没有的话把对应原单词存于一个二维数组中,对二维数组排序后输出。
#include<iostream> #include<cstdio> #include<string.h> #include<stdlib.h> #include<cmath> using namespace std; char word[100][50]; char tmp[100][50]; char ans[100][50]; int num[100]; int tot = 0; int cmp(const void*a,const void*b) { return *(char*)a - *(char*)b; } int comp(const void*a,const void*b) { return strcmp((char*)a,(char*)b); } int main() { while(scanf("%s", &word[tot]) && strcmp(word[tot], "#")) { for (int i = 0; i < strlen(word[tot]); i++) if (word[tot][i] >= 'A' && word[tot][i] <= 'Z') tmp[tot][i] = word[tot][i] - 'A' + 'a'; else tmp[tot][i] = word[tot][i]; qsort(tmp[tot], strlen(tmp[tot]), sizeof(tmp[tot][0]), cmp); tot++; } for (int i = 0; i < tot - 1; i++) for (int j = i + 1; j < tot; j++) if (strcmp(tmp[i], tmp[j]) == 0) { num[i]++; num[j]++; } int j = 0; for (int i = 0; i < tot; i++) if(num[i] == 0) strcpy(ans[j++], word[i]); qsort(ans, j, sizeof(ans[0]), comp); for (int i = 0 ; i < j; i++) printf("%s\n", ans[i]); return 0; }
相关文章推荐
- 1.10055 - Hashmat the brave warrior
- 2.10071 - Back to High School Physics
- 3.458 - The Decoder
- 4.694 - The Collatz Sequence
- 6.494 - Kindergarten Counting Game
- 7.490 - Rotating Sentences
- 8.414 - Machined Surfaces
- 9.488 - Triangle Wave
- A.457 - Linear Cellular Automata
- B.489 - Hangman Judge
- C.445 - Marvelous Mazes
- 1.10494 - If We Were a Child Again
- 2.424 - Integer Inquiry
- 3.10250 - The Other Two Trees
- 5.465 - Overflow
- 6.113 - Power of Cryptography
- 7.10161 - Ant on a Chessboard
- 8.621 - Secret Research
- 9.401 - Palindromes
- A.537 - Artificial Intelligence?