루미 알고리즘

#1390플래티넘Fibonacci Strings

PO 2016 훈련 캠프코딩출제 Johan Sannemo영어 원문이 문제가 어렵다면 — 배우는 순서 보기

영어 원문입니다. 원문의 수식·표를 글자로 옮겨 모양이 다를 수 있습니다. 정확한 내용은 원문 페이지에서 확인하세요. 원문 페이지 열기 (새 창)

Problem

Aron likes Fibonacci numbers. He likes them so much he actually got bored of the numbers themselves and decided to invent other combinatorial objects based on them instead.

His first invention is the Fibonacci string. We call a string consisting of only letters a and b, with exactly n letters a without any two consecutive letters a a Fibonacci string of the n:th order.

Given a string X of a:s and b:s, compute the sum of orders of all Fibonacci strings that are substrings of X. Note that if a string X appears multiple times, its order should be counted once for every occurance.

Example

Assume that Aron has the string abaaba. This contains the Fibonacci string a four times, ab two times, ba two times and aba two times. The sum of their orders is 4 · 1 + 2 · 1 + 2 · 1 + 2 · 2 = 4 + 2 + 2 + 4 = 12.

Task

Write a program that given X computes the sum of orders of all Fibonacci substrings of X. You should implement the function fibonacci(N, X).

• fibonacci(N, X) - this function will be called exactly once by the judge.

  • N: an integer - the number of charaters in the string X.
  • X: a string consisting of X characters, consisting only lowercase letters a and b.
  • The function should return an integer, the sum of orders of all Fibonacci substrings of X.

A code skeleton containing the function to be implemented, together with a sample grader, can be found at http://progolymp.se/uploads/kattis-attachments/fibonacci.zip.

Subtasks

The problem consists of a number of subtasks. Each subtask gives some amount of points, and to pass the subtask you must pass all the test cases in the subtask.

SubtaskPointsLimits
191 ≤ N ≤ 100
2111 ≤ N ≤ 5000
3141 ≤ N ≤ 100000. Each letter of X was chosen randomly with uniform probability.
4251 ≤ N ≤ 100000. X is a Fibonacci string.
5411 ≤ N ≤ 100000

Input format

The sample judge reads input in the following format:

  • line 1: N
  • line 2: X

Output format

The judge writes a single line containing the return value of fibonacci(N, X).

예제

예제 1

입력
6
abaaba
출력
12

출처 표기

출처: Programmeringsolympiaden 2016년 훈련 캠프 · 원문 페이지 · 이용 조건 CC BY-SA · LaTeX 원문을 화면용 글로 옮겼습니다(수식·표의 모양이 다를 수 있습니다). 내용은 고치지 않았습니다.

크리에이티브 커먼즈 저작자표시-동일조건변경허락.

문제 출처: 한국정보올림피아드(KOI) (CC BY-NC-SA 4.0) · International Olympiad in Informatics (IOI) (CC BY) · Centre for Education in Mathematics and Computing (CEMC), University of Waterloo (CC BY-NC 4.0) · Programmeringsolympiaden (CC BY-SA) · CSES Problem Set (Antti Laaksonen) (CC BY-NC-SA 4.0). 루미 알고리즘은 위 단체들과 관계없는 별도의 서비스이며, 자료는 원문 그대로 싣습니다. 문제마다 출처와 이용 조건을 따로 표기합니다.