UVA454 Anagrams【set+排序】

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 input. Each line constitutes one phrase.
Output
For each test case, 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 outputs for two consecutive input cases are 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

问题链接UVA454 Anagrams
问题简述:(略)
问题分析
    给定若干字符串,输出若干字符串组,其字母组成是相同的,用等号连接起来输出。
    简单题不解释。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的C++语言程序如下:

/* UVA454 Anagrams */

#include <bits/stdc++.h>

using namespace std;

int main()
{
    string line;
    int t;

    cin >> t;
    cin.ignore();
    cin.ignore();
    while(t--) {
        set<pair<string, string> > s;
        while(getline(cin, line) && line != "") {
            string t = "";
            for(int i = 0; line[i]; i++)
                if(!isspace(line[i]))
                    t += line[i];
            sort(t.begin(), t.end());

            s.insert(make_pair(line, t));
        }

        for(set<pair<string, string> >::iterator iter1 = s.begin(); iter1 != s.end(); iter1++) {
            set<pair<string, string> >::iterator iter2 = iter1;
            for(iter2++; iter2 != s.end(); iter2++)
                if(iter1->first != iter2->first && iter1->second == iter2->second)
                    cout << iter1->first << " = " << iter2->first << endl;
        }
        if(t) cout << endl;
    }

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值