Luogu-P6832 [Cnoi2020] Substring-Problem Solution

Luogu-P6832 [Cnoi2020] Substring-Problem Solution

Title description

Cirno has a string S, and hope you can find the number of occurrences of the non-empty substring with the most occurrences of S, denoted as p.

Input format

One line, one string S.

Output format

One line, an integer p.

Sample input and output

Enter #1

abababab

Output #1

4

Glossary

子串:字符串中任意个连续的字符组成的子序列称为该串的子串。

The first time I encountered this type of question, at first glance, it was really scary. (Especially konjac like me)
Actually, if you think about it, you will find an interesting phenomenon

  1. Example 1: ababab. The output 4 refers to the number of a.
  2. abcabcaa output 4-a
  3. abcabcbc output 3-bc

So far... I believe you must have seen a little pattern.

What is the law?
If the most string is single-character , the result is the largest number of characters (as in Example 1).
If the most string is double-character , then the two characters that make up the string must be greater than or equal to each character Quantity? For example, the third example above: the number of b and c is greater than the number of a.
What if it is a multi-character substring ? In fact, the principle is the same as the two-character string. The multi-character string is the most, so there must be one character in this string that is the most (at most refers to the number of characters greater than or equal to other characters), if an external character is greater than the max character in this string, then the answer is not this multi-character word Stringed

I believe you will understand what I mean after reading my illogical words!
This question can be simplified to find the character that appears the most, accumulate its number, and output

#include<iostream>
#include<string>
#include<algorithm>//快排头文件
using namespace std;
long long book[27];//book数组做计数作用,记录每个字符出现的次数
int main ()
{
    
    
	string n;
	cin >> n;
		long long len = n.length ();
		for ( long long i = 0; i < n.length (); i++ )
			book[n[i] - 'a']++;
		sort ( book, book+26 );//找出最大的那个字符
		cout << book[25] << endl;
	return 0;
}

Portal: Question

If you have a simpler idea, or if there are any flaws in my solution, I welcome your advice.

Guess you like

Origin blog.csdn.net/weixin_51550966/article/details/111184930