Title

map的应用——UVA156 反片语

map的应用——UVA156 反片语

题意翻译

题目大意 输入一些单词,找出所有满足如下条件的单词:该单词不能通过字母重排,得到输入文本的另外一个单词。在判断是否满足条件时,字母不分大小写,但在输出时应保留输入的大小写,按字典序排列。 翻译贡献者:很dalao的蒟蒻

输入输出样例

输入 #1

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
#

输出 #1

Disk
NotE
derail
drIed
eye
ladder
soon

思路

读入

将字符串一个一个读入,读到某个字符串的首个字母为‘#‘时跳出循环。

操作

  • 统计:对字符串进行标准化
    1. 转小写
    2. 排序
    3. 用一个map来统计字符串的个数(首先这个map是cnt <string, int >用来统计个数)
      1. 如果说这个字符串重来没有存在,即map名.count(r)==0;那么我们可以map名[新字符串]=1
      2. 如果本来就存在,就在原有的基础上加一,map名[字符串]++
    4. 根据题目要求,先对答案字符串提取,并对其(注意是原来的字符串)进行排序

代码

#include<iostream>
#include<stdio.h>
#include<map>
#include<vector>
#include<string.h>
#include<algorithm>
using namespace std;
map<string , int> s_num;
vector<string> wordlist;
vector<string> s_ans;
string standard(string s)
{
	string ans=s;
	for(int i=0;i<s.length();i++)
	{
		ans[i]=tolower(ans[i]);
	}
	sort(ans.begin(),ans.end());
	return ans;
}
int main()
{
	string s;
    cin>>s;
	while(s[0]!='#')
	{
	    wordlist.push_back(s);
	    string new_s=standard(s);
	    if(!s_num.count(new_s))
		{s_num[new_s]=1;
		}
	    else s_num[new_s]++;
		cin>>s;	   
	}
	
	for(int i=0;i<wordlist.size();i++)
	{
		if(s_num[standard(wordlist[i])]==1)
		   s_ans.push_back(wordlist[i]);
	}
	sort(s_ans.begin(),s_ans.end());
	for(int i=0;i<s_ans.size();i++)
	    cout<<s_ans.at(i)<<endl;
	return 0;
}

其他

函数返回类型

函数可以接收string的返回值

容器类型的sort用法

sort(容器名.begin,容器名.end)

转小写操作

新字符=tolower(旧字符)

posted @ 2021-04-02 22:15  BeautifulWater  阅读(49)  评论(0编辑  收藏  举报