zoukankan      html  css  js  c++  java
  • UVa: 156 Ananagrams

     

     Ananagrams 

    Most crossword puzzle fans are used to anagrams--groups of words with the same letters in different orders--for example OPTS, SPOT, STOP, POTS and POST. Some words however do not have this attribute, no matter how you rearrange their letters, you cannot form another word. Such words are called ananagrams, an example is QUIZ.

    Obviously such definitions depend on the domain within which we are working; you might think that ATHENE is an ananagram, whereas any chemist would quickly produce ETHANE. One possible domain would be the entire English language, but this could lead to some problems. One could restrict the domain to, say, Music, in which case SCALE becomes a relative ananagram (LACES is not in the same domain) but NOTE is not since it can produce TONE.

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

    Input

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

    Output

    Output will consist of a series of lines. Each line will consist of a single word that is a relative ananagram in the input dictionary. Words must be output in lexicographic (case-sensitive) order. There will always be 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  drIed
    noel dire Disk mace Rob dries
    #

    Sample output

    Disk
    NotE
    derail
    drIed
    eye
    ladder
    soon
    第一次提交就AC了,可能效率有点问题,呵呵
    #include <iostream>
    #include <string>
    #include <algorithm>
    #include <vector>
    #include <iterator>
    //#include "boost/foreach.hpp"
    using namespace std;
    
    struct sort_op{
    	bool operator() (string s1,string s2){
    		string::iterator it;
    		it=s1.begin();
    		while(it!=s1.end())
    		{
    			if(*it>='A'&&*it<='Z')
    			{
    				*it+=32;
    			}
    			++it;
    		}
    		it=s2.begin();
    		while(it!=s2.end())
    		{
    			if(*it>='A'&&*it<='Z')
    			{
    				*it+=32;
    			}
    			++it;
    		}
    		sort(s1.begin(),s1.end());
    		sort(s2.begin(),s2.end());
    		return s1<s2;
    	}
    };
    struct equal_op{
    	bool operator()(string s1,string s2){
    		string::iterator it;
    		it=s1.begin();
    		while(it!=s1.end())
    		{
    			if(*it>='A'&&*it<='Z')
    			{
    				*it+=32;
    			}
    			++it;
    		}
    		it=s2.begin();
    		while(it!=s2.end())
    		{
    			if(*it>='A'&&*it<='Z')
    			{
    				*it+=32;
    			}
    			++it;
    		}
    		sort(s1.begin(),s1.end());
    		sort(s2.begin(),s2.end());
    		return s1==s2;
    	}
    };
    
    int main()
    {
    	string str;
    	vector<string> v;
    	while(cin>>str)
    	{
    		if(str == "#") break;
    		v.push_back(str);
    	}
    	sort(v.begin(),v.end(),sort_op());
    	ostream_iterator<string> os(cout,"\n");
    	vector<string> v2;
    	if(v.size() == 1) cout<<v[0]<<endl;
    	else
    	{
    		int pre = 0;
    		int next = pre+1;
    		int len = v.size();
    		equal_op op;
    		do{
    			while(next<len && op(v[next],v[pre])) ++next;
    			if(next-1==pre){
    				v2.push_back(v[pre]);
    
    			}
    			if(next == len) break;
    			pre = next;
    			next++;
    		}while(true);
    	}
    	sort(v2.begin(),v2.end());
    	copy(v2.begin(),v2.end(),os);
    	return 0;
    }
    

     下面这个比较好啵

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <cstdlib>
    
    using namespace std;
    
    char str[100][25];
    char word[100][25];
    
    int cmp_string(const void *_a, const void *_b)
    {
        char *a = (char*)_a;
        char *b = (char*)_b;
        return strcmp(a,b);
    }
    
    int cmp_char(const void *_a, const void *_b)
    {
        char *a = (char*)_a;
        char *b = (char*)_b;
        return *a - *b;
    }
    
    int main()
    {
        char tp[25];
        int sl = 0;
        while (scanf("%s", tp), tp[0] != '#')
        {
            strcpy(str[sl++], tp);
        }
        qsort(str, sl, sizeof(str[0]), cmp_string);
        for (int i = 0; i < sl; i++)
        {
            int len = strlen(str[i]);
            for (int j = 0; j < len; j++)
            {
                if ('A' <= str[i][j] && 'Z' >= str[i][j])
                    word[i][j] = str[i][j] + ('a' - 'A');
                else
                    word[i][j] = str[i][j];
            }
            qsort(word[i], len, sizeof(char), cmp_char);
        }
        for (int i = 0; i < sl; i++)
        {
            int num = 0;
            for (int j = 0; j < sl; j++)
            {
                if (!strcmp(word[i], word[j]))
                    num++;
            }
            if (num == 1)
                printf("%s\n", str[i]);
        }
    
        return 0;
    }
    
  • 相关阅读:
    性能测试培训:性能瓶颈分析思路
    (国内)完美下载Android源码Ubuntu版
    (国内)完美下载android源代码(文章已经丢失)
    【翻译】Ext JS最新技巧——2015-10-21
    ubuntu 中 eclipse 的菜单栏 显示问题
    谷歌代码库已超过 20 亿行代码,他们是如何管理的?
    架构方面的资料集锦
    Android Studio 使用 Gradle 打包 Jar
    【翻译】Ext JS最新技巧——2015-8-11
    【翻译】在Ext JS 6通用应用程序中使用既共享又特定于视图的代码
  • 原文地址:https://www.cnblogs.com/UnGeek/p/2591911.html
Copyright © 2011-2022 走看看