题解 CF1025A 【Doggo Recoloring】
水题!
STL大法好!
先来分析一下,可以得知,只要有一种字符出现过两次以上,就可以和别的字符合并,就是Yes,注意字符串长度为1也是Yes
那怎么统计呢?对了,map!!!
代码如下(char[]和unordered_map和string都有哦):
#include <iostream>
#include <cstring>
#include <unordered_map>
using namespace std;
unordered_map <char, int> mp;
string a;
int b;
int main()
{
cin >> b >> a;
int x = a.length();
for(int i = 0; i <= x - 1; i++)
{
mp[a[i]]++;
}
for(unordered_map <char, int>::iterator it = mp.begin(); it != mp.end(); ++it)
{
if(it -> second >= 2)
{
cout << "Yes\n";
return 0;
}
}
cout << (x == 1 ? "Yes" : "No") << endl;
return 0;
}
以上是string+unordered_map
#include <iostream>
#include <cstring>
#include <map>
using namespace std;
map <char, int> mp;
string a;
int b;
int main()
{
cin >> b >> a;
int x = a.length();
for(int i = 0; i <= x - 1; i++)
{
mp[a[i]]++;
}
for(map <char, int>::iterator it = mp.begin(); it != mp.end(); ++it)
{
if(it -> second >= 2)
{
cout << "Yes\n";
return 0;
}
}
cout << (x == 1 ? "Yes" : "No") << endl;
return 0;
}
map+string的
#include <iostream>
#include <cstring>
#include <map>
using namespace std;
map <char, int> mp;
char a[100005];
int b;
int main()
{
cin >> b >> a;
int x = strlen(a);
for(int i = 0; i <= x - 1; i++)
{
mp[a[i]]++;
}
for(map <char, int>::iterator it = mp.begin(); it != mp.end(); ++it)
{
if(it -> second >= 2)
{
cout << "Yes\n";
return 0;
}
}
cout << (x == 1 ? "Yes" : "No") << endl;
return 0;
}
char+map
#include <iostream>
#include <cstring>
#include <unordered_map>
using namespace std;
unordered_map <char, int> mp;
char a[100005];
int b;
int main()
{
cin >> b >> a;
int x = strlen(a);
for(int i = 0; i <= x - 1; i++)
{
mp[a[i]]++;
}
for(unordered_map <char, int>::iterator it = mp.begin(); it != mp.end(); ++it)
{
if(it -> second >= 2)
{
cout << "Yes\n";
return 0;
}
}
cout << (x == 1 ? "Yes" : "No") << endl;
return 0;
}
char+unordered_map
速度最快484ms,后来评测慢一些500多600ms的样子,unordered_map用hash的可能比map快一些,c++11的

浙公网安备 33010602011771号