Mixed Messages

시간 제한2초메모리 제한2048 MB

요약
최종 문자열이 주어질 때, 코드워드 spbsu를 포함한 메시지들의 문자를 서로 다른 메시지 사이에서만 인접 교환한 최소 횟수를 구한다.
난이도

보통10점 중 7점

유형
그리디, 정렬, 문자열, 이분 탐색
정답자
아직 제출이 없습니다

문제

Nikita received some messages today. One of the messages was the code word "spbsu". Before and after the code word, there may have been any number of other messages as well. The other messages are arbitrary strings of lowercase English letters.

All the messages were sent via a secret channel, one by one. So, the string in the channel initially was a concatenation of all the messages.

However, being secret, the channel may introduce noise: different messages may interfere with each other. Formally, the noise comes in form of swaps. In each swap, the channel selects and exchanges two adjacent letters in the string that initially belonged to different messages. For letters of any particular message, the relative order is preserved.

After all swaps, the resulting string is received by Nikita. Given the resulting string, find the minimum possible number of swaps made by the channel.

입력

The first line contains a single integer nn: the number of received characters (5≤n≤1055 \leq n \leq 10^5).

The next line contains a string ss consisting of nn lowercase English letters: the resulting string received by Nikita. It is guaranteed that this string is the result of the process described above.

출력

Output a single integer: the answer to the problem.

예제2

  1. 예제 1

    입력
    6
    spbssu
    
    예상 출력
    1
    
  2. 예제 2

    입력
    15
    spongebaseurban
    
    예상 출력
    11