题解 CF1073A 【Diverse Substring】

STL大法好!!!

这道题目其实 map + 一个 O(n2)\large \text{O}(n^2) 的暴力就好了。

我们用 map 是为了统计每个字符出现数量,然后枚举找最大值,与 length2\LARGE \frac{length}{2} 作比较,注意小于等于是可以的,一开始就错在这里。

然后除法除以2用右移就可以了。

代码:

#include <iostream>
#include <unordered_map>
#include <algorithm>
#include <cstdio>
#include <string>
using namespace std;

inline bool judge(const string& s, unordered_map<char, int>& mp)
{
	int half = s.length() >> 1, maxn = 0;
	unordered_map<char, int>::iterator it_end = mp.end();
	for (register unordered_map<char, int>::iterator it = mp.begin(); it != it_end; ++it)
	{
		maxn = max(it -> second, maxn);
	}
	return maxn <= half;
}

int main()
{
	scanf("%*d");
	string s;
	cin >> s;
	int len = s.length();
	len--;
	for (register int i = 0; i <= len; i++)
	{
		unordered_map<char, int> mp;
		string now = "";
		for (register int j = i; j <= len; j++)
		{
			now += s[j];
			mp[s[j]]++;
			if (judge(now, mp))
			{
				cout << "YES\n";
				cout << now << endl;
				//system("pause");
				return 0;
			}
		}
	}
	cout << "NO\n";
	//system("pause");
	return 0;
}
posted @ 2021-01-16 15:26  HappyBobb  阅读(6)  评论(0)    收藏  举报  来源