0% found this document useful (0 votes)
35 views

Task Magic (English) : Remarks: A Substring Is A Part of A Given String, Which Contains

Deni is struggling in her English class and decides to count the number of magical substrings in the text written on the board by her teacher to stay occupied. A magical substring is one where each letter of the alphabet occurs an equal number of times. Deni asks for help programming a solution to count the magical substrings for increasingly long texts. The program takes a string of N English letters as input and outputs the number of magical substrings modulo 1,000,000,007.

Uploaded by

adinaise
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views

Task Magic (English) : Remarks: A Substring Is A Part of A Given String, Which Contains

Deni is struggling in her English class and decides to count the number of magical substrings in the text written on the board by her teacher to stay occupied. A magical substring is one where each letter of the alphabet occurs an equal number of times. Deni asks for help programming a solution to count the magical substrings for increasingly long texts. The program takes a string of N English letters as input and outputs the number of magical substrings modulo 1,000,000,007.

Uploaded by

adinaise
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

EJOI Day 1

Task Magic (English)


Now is the English lesson in grade 9 with Mr. Daskalov. Our main
character – Deni – is very weak in English and she counts the number of
flies in the room. This proves to be very boring activity, so she looks at the
board where the teacher has written some text. She ignores the spaces
between the words so the whole text seems to her as one big sequence of
English letters with length N. Let us denote the number of different
characters in this sequence with K. Deni starts to take up different
substrings from this sequence and she writes down the number of times
each character occurs. When for all of the K letters these numbers are
equal, she calls the current substring magical.
Remarks: A substring is a part of a given string, which contains
consecutively written characters.
During this English lesson she is able to check every substring of the
sequence. Meanwhile she has counted how many of the substrings are
magical and in the end she is very happy with the accomplished activity.
Deni decides that she would like to do so in every English lesson. But with
every subsequent English lesson the text on the board written by Mr.
Daskalov is becoming longer and longer. So she asks for your help – you
have to write a program which tells her the number of magical substrings
in a given sequence of N English letters.

Task
Write a program magic which counts the number of magical
substrings in a given sequence of N English letters. Substrings which are
the same but on different positions are counted as different

Input
From the first line of the standard input, your program has to read
one integer N – the number of characters in the sequence written by Mr.
Daskalov. From the next line your program has to read a string of N English
letters. The letters can be lower- and uppercase. Note that the lower- and
uppercase forms of the same letter are considered to be different
characters (A and a are different characters).
Output
The program must print to the standard output the number of
magical substrings in the given string. Since this number may be quite
large, you are required to print its remainder when divided by
1 000 000 007.
Constraints
 2 ≤ N ≤ 100 000

Task Magic Page 1 of 2


EJOI Day 1
Task Magic (English)

Subtasks
Subtas
Points N Further constraints
k
1 10 ≤ 100 There are no further constraints.
2 20 ≤ 2000 There are no further constraints.
≤ 100 There are only two kinds of characters in
3 30
000 the given string (K=2).
≤ 100 There are no further constraints.
4 40
000

Examples
Sample Input Sample Explanation
Output
8 4 The magical substrings are
abccbabc abc, cba, abc and abccba.
Note that for example the
substring ab is not magical
because the letter c is
not in it.
7 1 Only the substring abcABC
abcABCC is magical (the letters a and
A are different because a is
a lowercase letter and A is
an uppercase letter).
20 22 The number of magical
SwSSSwwwwSwSwwSww substrings is 22 and one of
wwS them is SwSwwS.

Task Magic Page 2 of 2

You might also like