#include <iostream>
#include <vector>
#include <cmath>
#include <map>
#include <string>
#include <algorithm> // for sort
using namespace std;
int N;
vector<int> nums;
string str;
int k;
map<char, int> mymap; //字母,次数

vector<pair<char, int>> pairs;


void fun()
{
	int num = str.length();
	char tempChar;
	int tempInt = 1;
	for (int i = 1; i < num; i++)
	{
		if (str[i] == str[i - 1])
		{
			tempInt++;
		}
		else
		{
			if (tempInt > mymap[str[i - 1]])
			{
				mymap[str[i - 1]] = tempInt;
			}
			tempInt = 0;
		}
		if (i == num - 1 && str[i] == str[i - 1] && tempInt > mymap[str[i - 1]])
		{
			mymap[str[i - 1]] = tempInt+1;
		}
	}
}

int main() {
	getline(cin, str);
	//cin >> str;
	//getchar();
	cin >> k;
	for (int i = 65; i < 91; i++)
	{
		mymap[i] = 0;
		
	}
	fun();
	for (auto id : mymap)
	{
		pair<char, int> temp(id.first, id.second);
		pairs.push_back(temp);
	}
	sort(pairs.begin(), pairs.end(), [](const pair<int, int>& a, const pair<int, int>& b) {
		return a.second > b.second;
	});
	//sort(pairs.end(), pairs.begin());   //此排序用法错误,需要自定义排序

	cout << pairs[k - 1].second<<endl;
	return 0;
}


/*
input:
LALALAHAHAMAKABBBAKAA
2

output:
2
*/

1 条评论

  • @ 2025-3-12 15:10:03

    修改了一下应该没问题了。

    #include <iostream>
    #include <vector>
    #include <cmath>
    #include <map>
    #include <string>
    #include <algorithm> // for sort
    using namespace std;
    int N;
    vector<int> nums;
    string str;
    int k;
    map<char, int> mymap; //字母,次数
    
    vector<pair<char, int>> pairs;
    
    
    void fun()
    {
    	int num = str.length();
    	char tempChar;
    	int tempInt = 1;
    	tempInt = 0;
    	tempChar = str[0];
    	for (int i = 0; i < num; i++)
    	{	
    		tempChar = str[i];
    		tempInt = 0;
    		while (i < num)
    		{
    			if (tempChar == str[i])
    			{
    				tempInt++;
    				i++;
    			}
    			else
    			{
    				i--;
    				break;
    			}
    		}
    		if (tempInt > mymap[tempChar])
    		{
    			mymap[tempChar] = tempInt;
    		}
    	}
    }
    
    int main() {
    	getline(cin, str);
    	//cin >> str;
    	//getchar();
    	cin >> k;
    	for (int i = 65; i < 91; i++)
    	{
    		mymap[i] = 0;
    		
    	}
    	fun();
    	for (auto id : mymap)
    	{
    		pair<char, int> temp(id.first, id.second);
    		pairs.push_back(temp);
    	}
    	sort(pairs.begin(), pairs.end(), [](const pair<int, int>& a, const pair<int, int>& b) {
    		return a.second > b.second;
    	});
    	//sort(pairs.end(), pairs.begin());   //此排序用法错误,需要自定义排序
    
    	cout << pairs[k - 1].second;
    	return 0;
    }
    
    
    /*
    input:
    LALALAHAHAMAKABBBAKAA
    2
    
    output:
    2
    
    
    ABC
    2
    */
    
    • 1

    信息

    ID
    51
    时间
    1000ms
    内存
    256MiB
    难度
    4
    标签
    递交数
    175
    已通过
    77
    上传者