2024-07-06
한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina
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.
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.
The data range is large, so we need to control the time complexity to O ( n log n ) O(nlog n) O(nlogn) Within.
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 a…b 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.
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 a…b All are legal strings. Count the characters after a certain position b b b The number ofSuffix andThe algorithm is maintained.
#include