310443: CF1834C. Game with Reversing

Memory Limit:256 MB Time Limit:1 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

C. Game with Reversingtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard output

Alice and Bob are playing a game. They have two strings $S$ and $T$ of the same length $n$ consisting of lowercase latin letters. Players take turns alternately, with Alice going first.

On her turn, Alice chooses an integer $i$ from $1$ to $n$, one of the strings $S$ or $T$, and any lowercase latin letter $c$, and replaces the $i$-th symbol in the chosen string with the character $c$.

On his turn, Bob chooses one of the strings $S$ or $T$, and reverses it. More formally, Bob makes the replacement $S := \operatorname{rev}(S)$ or $T := \operatorname{rev}(T)$, where $\operatorname{rev}(P) = P_n P_{n-1} \ldots P_1$.

The game lasts until the strings $S$ and $T$ are equal. As soon as the strings become equal, the game ends instantly.

Define the duration of the game as the total number of moves made by both players during the game. For example, if Alice made $2$ moves in total, and Bob made $1$ move, then the duration of this game is $3$.

Alice's goal is to minimize the duration of the game, and Bob's goal is to maximize the duration of the game.

What will be the duration of the game, if both players play optimally? It can be shown that the game will end in a finite number of turns.

Input

Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.

The first line of each test case contains a single integer $n$ ($1 \le n \le 10^5$) — the length of the strings $S$ and $T$.

The second line of each test case contains a string $S$ of length $n$ consisting of lowercase latin letters.

The third line of each test case contains a string $T$ of length $n$ consisting of lowercase latin letters.

It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$.

Output

For each test case, output a single number on a separate line — the duration of the described game, if both players play optimally.

ExampleInput
7
5
abcde
abxde
5
hello
olleo
2
ab
cd
7
aaaaaaa
abbbbba
1
q
q
6
yoyoyo
oyoyoy
8
abcdefgh
hguedfbh
Output
1
2
3
9
0
2
6
Note

In the first test case, in her turn, Alice can replace the third symbol of the string $S$ with x. After that, both strings will become equal to "abxde" and the game will end after one move. Since Alice's goal is to finish the game in as few moves as possible, this move will be one of her optimal first moves, and the final answer will be $1$.

In the second test case, in her turn, Alice can replace the fifth symbol of the string $T$ with h. After this move, $S =$ "hello", $T =$ "olleh". Then Bob makes his turn. In his turn, he must reverse one of the strings. If Bob chooses the string $S$, then after his turn both strings will be equal to "olleh", and if he chooses the string $T$, then after his turn both strings will be equal to "hello". Thus, after the presented first move of Alice, the game will definitely end in $2$ moves. It can be shown that there is no strategy for Alice to finish the game in less than $2$ moves, with both players playing optimally. The final answer is $2$.

In the third test case, in her first move, Alice can replace the second symbol of the string $S$ with c. After this move, $S =$ "ac", $T =$ "cd". Then Bob makes his turn. If Bob reverses the string $S$, then after his turn $S =$ "ca", $T =$ "cd". Then it is easy to see that in this case Alice can definitely finish the game on the $3$-rd move, by replacing the second symbol of the string $T$ with a, after which both strings will become equal to "ca". If Bob reverses the string $T$, then after his turn $S =$ "ac", $T =$ "dc". In this case, Alice can also definitely finish the game on the $3$rd move, by replacing the first symbol of the string $S$ with d, after which both strings will become equal to "dc". Thus, Alice can definitely finish the game in $3$ moves regardless of Bob's moves. It can be shown that the game cannot end in less than $3$ moves, with both players playing optimally.

In the fifth test case, the strings $S$ and $T$ are equal, so the game will end without starting, in $0$ moves.

Input

题意翻译

小 L 和小 S 在玩游戏。他们有两个长度均为 $n(1 \le n \le 10^5)$ 的字符串 $S, T$,小 L 和小 S 轮流操作,小 L 先手。 小 L 的回合,他可以选择 $1 \to n$ 中的一个整数 $i$,再选择串 $S$ 或 $T$,把其中的第 $i$ 个字符改成任意一个字符 $c$。 小 S 的回合,她可以选择 $S$ 或 $T$,并将它进行翻转。 如果两个串 $S, T$ 经过若干次操作后相等,则游戏立即结束。小 L 希望总操作次数最小,小 S 希望总操作次数最大。两人都采取最优策略,问最终操作次数。

Output

题目大意:
Alice和Bob在玩游戏。他们有两个相同长度n的字符串S和T,由小写拉丁字母组成。玩家交替进行,Alice先手。

Alice的回合,她选择一个整数i(1到n),字符串S或T中的一个,以及任何小写拉丁字母c,并用字符c替换所选字符串的第i个符号。

Bob的回合,他选择字符串S或T,并反转它。更正式地说,Bob进行替换S:=rev(S)或T:=rev(T),其中rev(P)=P_nP_{n-1}…P_1。

游戏持续到字符串S和T相等。字符串一相等,游戏立即结束。

定义游戏时长为游戏过程中两位玩家总共进行的移动次数。例如,如果Alice总共进行了2次移动,Bob进行了1次移动,则游戏时长为3。

Alice的目标是最小化游戏时长,而Bob的目标是最大化游戏时长。

如果两位玩家都发挥最佳,游戏时长将是什么?可以证明游戏将在有限回合内结束。

输入输出数据格式:
每个测试包含多个测试用例。第一行包含测试用例数t(1≤t≤10^4)。接下来是测试用例的描述。

每个测试用例的第一行包含一个整数n(1≤n≤10^5)——字符串S和T的长度。

每个测试用例的第二行包含一个长度为n的字符串S,由小写拉丁字母组成。

每个测试用例的第三行包含一个长度为n的字符串T,由小写拉丁字母组成。

保证所有测试用例的n之和不超过10^5。

对于每个测试用例,输出一行——如果两位玩家都发挥最佳,所描述游戏的时长。题目大意: Alice和Bob在玩游戏。他们有两个相同长度n的字符串S和T,由小写拉丁字母组成。玩家交替进行,Alice先手。 Alice的回合,她选择一个整数i(1到n),字符串S或T中的一个,以及任何小写拉丁字母c,并用字符c替换所选字符串的第i个符号。 Bob的回合,他选择字符串S或T,并反转它。更正式地说,Bob进行替换S:=rev(S)或T:=rev(T),其中rev(P)=P_nP_{n-1}…P_1。 游戏持续到字符串S和T相等。字符串一相等,游戏立即结束。 定义游戏时长为游戏过程中两位玩家总共进行的移动次数。例如,如果Alice总共进行了2次移动,Bob进行了1次移动,则游戏时长为3。 Alice的目标是最小化游戏时长,而Bob的目标是最大化游戏时长。 如果两位玩家都发挥最佳,游戏时长将是什么?可以证明游戏将在有限回合内结束。 输入输出数据格式: 每个测试包含多个测试用例。第一行包含测试用例数t(1≤t≤10^4)。接下来是测试用例的描述。 每个测试用例的第一行包含一个整数n(1≤n≤10^5)——字符串S和T的长度。 每个测试用例的第二行包含一个长度为n的字符串S,由小写拉丁字母组成。 每个测试用例的第三行包含一个长度为n的字符串T,由小写拉丁字母组成。 保证所有测试用例的n之和不超过10^5。 对于每个测试用例,输出一行——如果两位玩家都发挥最佳,所描述游戏的时长。

加入题单

上一题 下一题 算法标签: