UVa 156 ananagrams: Using STL multimap&set to process duplicate elements in a dictionary

Source: Internet
Author: User
Tags insert relative time limit

156-ananagrams

Time limit:3.000 seconds

Http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=98&page=show_problem &problem=92

Most crossword puzzle fans are used to anagrams--groups of words with the same letters in different orders--for example OP TS, SPOT, STOP, POTS and POST. Some words however don't have this attribute, no matter how to you rearrange their letters, you cannot form another word. Such words are called ananagrams, a example is quiz.

Obviously such definitions depend on the domain within which we are working; You might ' Athene is a ananagram, whereas any chemist would quickly produce. One possible domain would be the entire 中文版 language, but this could leads to some problems. One could restrict the domain to, say, Music, into which case SCALE becomes a relative ananagram (laces isn't in the same d Omain) But note isn't since it can produce TONE.

Write a program that would read in the dictionary of a restricted domain and determine the relative ananagrams. Note This single letter words are, ipso facto, relative the ananagrams since they be ' cannot ' to all. The dictionary would contain no more than 1000 words.

Input

Input would consist of a series of lines. No line is more than characters long and but may contain any number of words. Words consist of upper and/or lower case letters, and won't is broken across. Spaces may appear freely around words, and in least one space separates multiple to on the words line. Note This words that contain the same letters but of differing case are considered to is anagrams of each other, thus tied and EdiT are anagrams. The file would be terminated by a line consisting of a single #.

Output

Output would consist of a series of lines. Each line would consist of a single word this is a relative ananagram in the input dictionary. Words must is output in lexicographic (case-sensitive) order. There'll always is at least one relative ananagram.

Sample input

Ladder came tape soon leader Acme RIDE Lone Dreis Peat
 ScAlE orb  eye  rides dealer Note  derail laces  Dr Ied
Noel Dire Disk mace Rob Dries
#

Sample output

Disk
Note
derail
dried
eye
Ladder
soon

Train of thought: first use to establish a multimap< sort of word, unsorted word > To process input, finished will only one value key into a set, finally traverse set output. (see Code for details)

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

Complete code:

/*0.015s*/
    
#include <cstdio>  
#include <cstring>  
#include <string>  
#include < algorithm>  
#include <map>  
#include <set>  
using namespace std;  
    
Char s[25], origin[25];  
Multimap<string, string> map1;  
Set<string> Set1;  
Multimap<string, string>::iterator pm;  
Set<string>::iterator PS;  
    
int main ()  
{  
    int len, I;  
    while (scanf ("%s", s), s[0]!= ' # ')  
    {  
        strcpy (origin, s);  
        Len = strlen (s);  
        for (i = 0; i < len; ++i)  
            if (Isupper (S[i])) s[i] = ToLower (s[i));  
        Sort (s, S + len);  
        Map1.insert (Make_pair (S, origin));  
    for (PM = map1.begin (); PM!= map1.end (); ++pm)  
        if (Map1.count (pm->first) = = 1)  
            Set1.insert (Pm->second); For  
    (PS = Set1.begin (); PS!= set1.end (); ++ps)  
        puts ((*ps). C_STR ());  
    return 0;  
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.