24h購物| | PChome| 登入
2012-07-16 07:47:04| 人氣1,128| 回應0 | 上一篇 | 下一篇

[UVA] 454 - Anagrams

推薦 0 收藏 0 轉貼0 訂閱站台


 Anagrams 

An anagram is a word or phrase formed by rearranging the letters of another word or phrase. For example, ``carthorse" is an anagram of ``orchestra". Blanks within a phrase are ignored in forming anagrams. Thus, ``orchestra" and ``horse cart" are also anagrams.

Write a program that reads a list of phrases and prints all pairs of anagrams occurring in the list.

Input

The input file will contain a single integer at the first line of the input, indicate the number of test case you need to test followed by a blank line. Each test case will consist of from 1 to 100 lines. A completely empty or blank line signals the end of a test case. Each line constitutes one phrase.

Output

Some number of lines (including possibly 0 if there are no anagrams in the list), each line containing two anagrammatic phrases separated by `='.

Each anagram pair should be printed exactly once, and the order of the two phrases within a printed pair must be lexicographic, as well as the first phrases and the second ones in case some first are equal.

Two consecutive output for two consecutive input is separated by a single blank line.

Sample Input

1

carthorse
horse
horse cart
i do not know u
ok i now donut
orchestra

Sample Output

carthorse = horse cart
carthorse = orchestra
horse cart = orchestra
i do not know u = ok i now donut



#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <algorithm>
#include <iostream>
using namespace std;
int main() {
int t;
scanf("%d ", &t);
while(t--) {
string line[101];
int idx = 0, word[101][128] = {};
int i, j;
while(getline(cin, line[idx])) {
if(line[idx] == "")
break;
idx++;
}
sort(line, line+idx);
for(j = 0; j < idx; j++) {
for(i = 0; line[j][i]; i++) {
if(isalpha(line[j][i])) {
word[j][line[j][i]]++;
}
}
}
for(i = 0; i < idx; i++) {
for(j = i+1; j < idx; j++) {
if(!memcmp(word[i], word[j], sizeof(word[i]))) {
printf("%s = %s\n", line[i].c_str(), line[j].c_str());
}
}
}
if(t)
puts("");
}
return 0;
}

台長: Morris
人氣(1,128) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA] 630 - Anagrams (II)
此分類上一篇:[UVA][dfs] 148 - Anagram checker

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文