1054 The Dominant Color

Posted CSU迦叶

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了1054 The Dominant Color相关的知识,希望对你有一定的参考价值。

1. 此题用到了map<string,int>将输入的颜色(long long也存不下,只好作为string存入)的次数记录,看来默认一个没出现过的string对应的int是0。因此记次数的时候

if(mp[str])mp[str] += 1;//如果不是第一次出现,出现次数+1 
else mp[str] = 1;//如果是第一次出现,记出现次数为1 

2. 用set<string>存所有输入的字符串,利用其去重遍历的功能。

#include<cstdio>
#include<map>
#include<set>
#include<string>
#include<cstring>
#include<iostream>

using namespace std;


int main(){
	
	int row,col;
	
	scanf("%d %d",&col,&row);
	
	map<string,int> mp;
	set<string> st;
	
	string str;
	for(int i=0;i<row;i++){
		for(int j=0;j<col;j++){
			cin>>str;
			st.insert(str);
			if(mp[str])mp[str] += 1;//如果不是第一次出现,出现次数+1 
			else mp[str] = 1;//如果是第一次出现,记出现次数为1 
		}
	}
	
	int max = 0;
	string color; 
	set<string>::iterator it = st.begin(); 
	for(;it!=st.end();it++){
		if(max<mp[*it]){
			max = mp[*it];
			color = *it;
		}
	}
	
	cout<<color<<endl;
	
	return 0;
}

以上是关于1054 The Dominant Color的主要内容,如果未能解决你的问题,请参考以下文章