UVA156-5.4-Ananagrams-映射(map)

UVA156-5.4-Ananagrams-映射(map)
题目描述:
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=835&page=show_problem&problem=3248
输入一些单词,找出所有满足如下条件的单词:该单词不难通过字母重排,得到输入文本中的另外一个单词。在判断是否满足条件时,字母不分大小写,但在输出时应保留输入中的大小写,按字典序进行排序(所有大写字母在所有小写字母的前面)。
题目分析:
这是紫书上的一道例题==不是很懂C++,所以基本就是按着课本来写的,这里使用了一个新东西——映射:map。例如可以用一个

map<string,int>month_name

来表示“月份名字到月份编号”的映射,然后用month_name[“july”]=7这样的方式来赋值。
代码中还有一个就是vector<sting> words感觉这个东西有点玄学=。=不是很懂,难道是长和宽都不固定的二维字符串?这是我猜到=、=
另外代码中还用的了诸如ans.begin(),ans.end()之类的东西,这两个函数指向了首地址和尾地址。
给出代码:

#include <iostream>
#include <string>
#include <cctype>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;

map<string,int> cnt;
vector<string> words;

string reper(const string& s)
{
    int i;
    string ans=s;
    for(i=0;i<ans.length();i++)
    {
        ans[i]=tolower(ans[i]);
    }
    sort(ans.begin(),ans.end());
    return ans;
}

int main()
{
    int n=0;
    string s;
    while(cin>>s)
    {
        if(s[0]=='#')
            break;
        words.push_back(s);
        string r=reper(s);
        if(!cnt.count(r))
            cnt[r]=0;
        cnt[r]++;
    }
    vector<string> ans;
    for(int i=0;i<words.size();i++)
        if(cnt[reper(words[i])]==1)
        ans.push_back(words[i]);
    sort(ans.begin(),ans.end());
    for(int i=0;i<ans.size();i++)
        cout<<ans[i]<<"\n";
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值