Technology Sharing

Solution to Problem G of Group CB of the 14th Lanqiao Cup Provincial Competition [Abbreviation of Substring] (AC)

2024-07-06

한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina

insert image description here
insert image description here
insert image description here
insert image description here
insert image description here
insert image description here

Title

Given a string s s s,character a , b a, b a,b, ask the string s s s How many a a a beginning b b b Ending substring.

Problem Solving

20pts

Use a double loop to enumerate the left and right endpoints and determine whether a a a beginning b b b If it is a string that ends with , then the answer increases by one.

100pts

The data range is large, so we need to control the time complexity to O ( n log ⁡ n ) O(nlog n) O(nlogn) Within.

Law 1

We need to find all a a a beginning b b b End of the string, then we can for each character b b b, go and see b b b There are several on the left a a a, then these a … b adots b ab is a legal string. Count the number of characters to the left of a certain position a a a, we can usePrefix andThe algorithm is maintained.

Method 2

We can traverse the entire string, for each a a a There are several characters to the right of the character b b b, then these a … b a dots b ab All are legal strings. Count the characters after a certain position b b b The number ofSuffix andThe algorithm is maintained.

#include