其他分享
首页 > 其他分享> > 例题5-4 UVA156 Ananagrams(19行AC代码)

例题5-4 UVA156 Ananagrams(19行AC代码)

作者:互联网

紫书刷题进行中,题解系列【GitHub|CSDN

例题5-4 UVA156 Ananagrams(19行AC代码)

题目大意

给定若干单词,按字典序输出不存在重排的单词。(经测试,不包含重复的单词

重排单词:每个字母出现次数一样,但顺序不同,即对单词序列的一个排序

思路分析

是否满足重排可转换为等价问题:单词的构成字母与顺序无关,有两种解决思路(标准化

map<map<char,int>, int>dict; // 每个单词的字母出现次数->出现次数
map<string, int> dict; // 标准化单词->出现次数

因此在标准化单词(全转为小写)后,边可按照不同思路统计次数,并记录相应到旧单词映射

遍历每个标准化单词,若其出现次数为1,插入ans集合中,最后输出即可

AC代码(C++11,map标准化,顺序无关)

统一排序

#include<bits/stdc++.h>
using namespace std;
map<string, int> dict; // 标准化单词->出现次数
map<string, string> trans; // 单词字母出现次数->原单词
set<string> ans; // 存储输出结果,按字典序排列
string s, st;
int main() {
    while (cin >>s && s!= "#") {
        st = s;
        for (auto& ch : st) // 转为小写
            if (ch >= 'A' && ch <= 'Z') ch = tolower(ch); // 转为小写
        sort(st.begin(), st.end()); // 升序排列,顺序无关
        dict[st]++; // 统计次数
        trans[st] = s; // 记录原单词
    }
    for (auto p : dict) if (p.second == 1) ans.insert(trans[p.first]); // 出现次数1表示非重排单词
    for (auto& p : ans) cout <<p <<endl; // 直接输出剩下的单词
    return 0;
}

字母计数

#include<bits/stdc++.h>
using namespace std;
map<map<char,int>, int>dict; // 每个单词的字母出现次数->出现次数
map<map<char,int>, string> trans; // 单词字母出现次数->原单词
set<string> ans; // 存储输出结果,按字典序排列
string s, st;
int main() {
    while (cin >>s && s!= "#") {
        st = s;
        map<char, int> mp;
        for (auto& ch : st) {
            if (ch >= 'A' && ch <= 'Z') ch = tolower(ch); // 转为小写
            mp[ch] ++; // 统计每个字符出现次数
        }
        dict[mp]++; // 统计次数
        trans[mp] = s; // 记录原单词
    }
    for (auto p : dict) if (p.second == 1) ans.insert(trans[p.first]); // 出现次数1表示非重排单词
    for (auto& p : ans) cout <<p <<endl; // 直接输出剩下的单词
    return 0;
}
是阿俊呐 发布了77 篇原创文章 · 获赞 83 · 访问量 2万+ 私信 关注

标签:map,AC,ch,UVA156,字母,st,单词,次数,例题
来源: https://blog.csdn.net/qq_40738840/article/details/104192559