Anagram字符串处理(STL真方便啊。。)

题意:给出一些字符串,认为各个字符个数相同的字符串就是相同的,不区分大小写,找出这些字符串中不与其他字符串相同的字符串并挨个输出

用char orgin[][]把每个字符串保存起来,然后对每个字符串都改成小写字母再排序到s[][],然后按字典序把字符串们排个序,记录序号相对顺序到int a[]中就好,然后按从小到大顺序遍历a中,输出与相邻的字符串不同的就好。然后这个用STL的话就很方便了,因为string默认就按字典序比较大小,用multimap以处理好的字符串为键,原始字符串为值,找到只有一个值的键放到set中,然后又自己排好序,遍历输出就好。。。

#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <map>
#include <set>
#include <ctype.h>

using namespace std;

char s[25], origin[25];
multimap<string, string>mp;
set<string>st;

int main()
{
    int len;
    while (~scanf("%s", origin) && origin[0] != '#') {
        strcpy(s, origin);
        len = strlen(s);
        for (int i = 0; i < len; i++)
            if(isupper(s[i])) s[i] = tolower(s[i]);
        sort(s, s+len);
        mp.insert(pair<string, string>(s, origin));//key and value
    }

    for (auto i:mp)
        if (mp.count(i.first) == 1)
            st.insert(i.second);
    for (auto i:st)
        printf("%s\n", i.c_str());
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/kangkang-tao/p/9486790.html