Problem 3065 --Play Games with Rounddog

3065: Play Games with Rounddog

"
Time Limit $8$ 秒/Second(s) Memory Limit $512$ 兆字节/Megabyte(s)
提交总数 $1$ 正确数量 $1$
裁判形式 标准裁判/Standard Judge 我的状态 尚未尝试
难度 分类标签
As a typical Nim game, picking stones is very famous all over the world with its quite simple rule. Initially, there are several piles of stones. Two players take turns to remove at least one stone from one of the piles. Whoever cannot make any legal move loses this game.

On August 17th, a very special day, Rounddog and Calabash create another picking stones game of their own. The new rule is as the following.

To start with, Calabash takes out a string S from his right pocket as the keystone of their game, which has m round in total.

At the beginning of each round, their common friend Severus will select a substring T from S. Then before they officially start playing, there are three phases need to be applied.

In Phase 1, Calabash will select several distinct substrings from S, satisfying that they all have a suffix T. Taking "claris" as an example, one of its suffixes is "ris". 

Phase 2 requires some magical power. Calabash will turn all strings he selects to stone piles. Specifically, for each string X he chooses, it will become a pile of Wpstones where p is the number of occurrences of X in S. For example, "aba" occurs in "ababa" for 2 times.

Rounddog will be in charge of Phase 3. After Severus and Calabash's movement, Rounddog chooses some piles from Calabash's selection, and throws them away. But Rounddog can't throw all the piles Calabash selected, because it will lead the game to the end immediately.

With the left piles, Rounddog and Calabash will start playing based on the original rule of Picking Stones. Calabash always moves first. 

Now, our beloved Quailty wants to know whether Calabash will win in each round if they both perform the optimal strategy. Furthermore, he also wants you to calculate the maximum total number of stones Calabash can achieve in Phase 2 on the premise of his victory.
The input contains several test cases, and the first line contains a single integer T (1≤T≤3), the number of test cases.

In each test case, the first line contains an integer n (1≤n≤100000).

The second line contains a string S of length n with only lowercase English letters.

The third line contains n integers, i-th of which is Wi (1≤Wi<258).

The fourth line contains an integer m (1≤m≤200000), representing the number of games.

Each of the next m lines contains two integers l and r (1≤l≤r≤n) meaning that in this game, Severus will select S[l,r] as T.
For each test case, output m lines, each of which contains an integer representing the maximum total number of stones Calabash can achieve on the premise of his victory, or −1 if he always loses.
1
5
aabab
1 3 5 7 9
5
1 1
1 2
2 2
2 3
3 5
6
1
6
4
1

推荐代码 查看3065 所有题解 上传题解视频得图灵币

本题记录 用 户(点击查看用户) 运行号(点击购买题解) 时 间
算法最快[$2616 $ms] 淡意的温柔 588049 2020-05-27 09:37:14
内存最少[$0 $KB] 淡意的温柔 588049 2020-05-27 09:37:14
第一AC 淡意的温柔 588049 2020-05-27 09:37:14
第一挑战 淡意的温柔 588049 2020-05-27 09:37:14

赛题来源/所属竞赛 2019 Multi-University Training Contest 10 N/A

竞赛编号 竞赛名称 竞赛时间 访问比赛