310372: CF1823D. Unique Palindromes

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

Description

D. Unique Palindromestime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard output

A palindrome is a string that reads the same backwards as forwards. For example, the string abcba is palindrome, while the string abca is not.

Let $p(t)$ be the number of unique palindromic substrings of string $t$, i. e. the number of substrings $t[l \dots r]$ that are palindromes themselves. Even if some substring occurs in $t$ several times, it's counted exactly once. (The whole string $t$ is also counted as a substring of $t$).

For example, string $t$ $=$ abcbbcabcb has $p(t) = 6$ unique palindromic substrings: a, b, c, bb, bcb and cbbc.

Now, let's define $p(s, m) = p(t)$ where $t = s[1 \dots m]$. In other words, $p(s, m)$ is the number of palindromic substrings in the prefix of $s$ of length $m$. For example, $p($abcbbcabcb$, 5)$ $=$ $p($abcbb$) = 5$.

You are given an integer $n$ and $k$ "conditions" ($k \le 20$). Let's say that string $s$, consisting of $n$ lowercase Latin letters, is good if all $k$ conditions are satisfied at the same time. A condition is a pair $(x_i, c_i)$ and have the following meaning:

  • $p(s, x_i) = c_i$, i. e. a prefix of $s$ of length $x_i$ contains exactly $c_i$ unique palindromic substrings.
Find a good string $s$ or report that such $s$ doesn't exist.

Look in Notes if you need further clarifications.

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 two integers $n$ and $k$ ($3 \le n \le 2 \cdot 10^5$; $1 \le k \le 20$) — length of good string $s$ and number of conditions.

The second line of each test case contains $k$ integers $x_1, x_2, \dots, x_k$ ($3 \le x_1 < x_2 < \dots < x_k = n$) where $x_i$ is the length of the prefix in the $i$-th condition.

The third line of each test case contains $k$ integers $c_1, c_2, \dots, c_k$ ($3 \le c_1 \le c_2 \le \dots \le c_k \le \min{\left(10^9, \frac{(n + 1) n}{2} \right)}$) where $c_i$ is the number of palindromic substrings in the $i$-th condition.

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

Output

For each test case, if there is no good string $s$ of length $n$ that satisfies all conditions, print NO.

Otherwise, print YES and a string $s$ of length $n$, consisting of lowercase Latin letters, that satisfies all conditions. If there are multiple answers, print any of them.

ExampleInput
7
10 2
5 10
5 6
3 1
3
3
4 2
3 4
3 3
4 2
3 4
3 4
4 1
4
5
10 3
4 6 10
4 5 8
10 4
4 6 7 10
4 5 7 8
Output
YES
abcbbcabcb
YES
foo
YES
ayda
YES
wada
NO
YES
abcbcacbab
NO
Note

In the first test case, string $s$ $=$ abcbbcabcb satisfies $k = 2$ conditions:

  • $p(s, x_1) = p(s, 5) =$ $p($abcbb$) = 5 = s_1$. Palindromic substrings are a, b, c, bb and bcb.
  • $p(s, x_2) = p(s, 10) =$ $p($abcbbcabcb$) = 6 = s_2$. Palindromic substrings are the same as above, and one extra substring cbbc.

In the second test case, string foo satisfies $k = 1$ condition:

  • $p($foo$) = 3$. Palindromic substrings are f, o and oo.
There are other possible answers.

In the third test case, string ayda satisfies $k = 2$ conditions:

  • $p($ayd$) = 3$. Palindromic substrings are a, y and d.
  • $p($ayda$) = 3$. Palindromic substrings are the same.

In the fourth test case, string wada satisfies $k = 2$ conditions:

  • $p($wad$) = 3$. Palindromic substrings are w, a and d.
  • $p($wada$) = 4$. Palindromic substrings are the same, and one extra substring ada.

In the fifth test case, it can be proven that there is no string of length $4$ which has $5$ palindromic substrings.

In the sixth test case, string abcbcacbab satisfies $k = 3$ conditions:

  • $p($abcb$) = 4$. Palindromic substrings are a, b, c and bcb.
  • $p($abcbca$) = 5$. Palindromic substrings are the same, and one extra substring cbc.
  • $p($abcbcacbab$) = 8$. Palindromic substrings are the same, and three extra substrings cac, bab and bcacb.

Input

题意翻译

令 $p(t)$ 表示字符串 $t$ 的不同回文子串数(不同即多次出现只算一次)。 令 $p(s,m)$ 表示字符串 $s$ 的 $m$ 前缀的不同回文子串数,即 $p(s,m)=p(t)$,其中 $t=s[1..m]$。 如:$t=\texttt{abcbbcabcb}$,则 $p(t)=6$($\texttt{a},\texttt{b},\texttt{c},\texttt{bb},\texttt{bcb},\texttt{cbbc}$),$p(t,5)=p(\texttt{abcbb})=5$($\texttt{a},\texttt{b},\texttt{c},\texttt{bb},\texttt{bcb}$)。 给定整数 $n$ 和 $k$ 个条件,第 $i$ 个条件用 $(x_i,c_i)$ 表示,意思是对于字符串 $s$,满足 $p(s,x_i)=c_i$。 请你构造一个由小写拉丁字母组成的长度为 $n$ 的字符串 $s$,使其满足所有的 $k$ 个条件。 #### 输入格式 本题有**多组数据**。第一行输入数据组数 $t$。 对于每组数据,第一行输入两个整数 $n$ 和 $k$,第二行输入 $k$ 个整数 $x_1,x_2,\dots,x_k$,第三行输入 $k$ 个整数 $c_1,c_2,\dots,c_k$。 #### 输出格式 对于每组数据: 如果不能构造满足所有 $k$ 个条件且长度为 $n$ 的字符串 $s$,输出一行 `NO`;否则,输出 `YES`,然后输出任意一种满足所有条件的 $s$。 #### 数据范围 $1\le t\le10^4$,$3\le n\le2\times10^5$,$1\le k\le20$。 $3\le x_1<x_2<\dots<x_k=n$,$3\le c_1\le c_2\le\dots\le c_k\le\min\left(10^9,\frac{(n+1)n}2\right)$。 每组数据的 $n$ 的总和 $\sum n\le2\times10^5$。

Output

题目大意:给定一个字符串t,求t中不同回文子串的个数。即使某些子串在t中多次出现,也只计算一次。定义p(t)为字符串t中不同回文子串的个数。例如,字符串t=abcbbcabcb有6个不同的回文子串:a, b, c, bb, bcb和cbbc。定义p(s, m)=p(t),其中t=s[1…m]。换句话说,p(s, m)是以s为前缀的长度为m的字符串中回文子串的个数。例如,p(abcbbcabcb, 5)=p(abcbb)=5。给定一个整数n和k个“条件”(k≤20)。如果一个由n个小写拉丁字母组成的字符串s同时满足所有k个条件,则称该字符串s为“好字符串”。一个条件是一个二元组(xi, ci),含义是:p(s, xi)=ci,即字符串s的前xi个字符包含ci个唯一的回文子串。找到一个好字符串s或报告这样的s不存在。

输入输出数据格式:

输入:

第一行包含一个整数t(1≤t≤10^4),表示测试用例的数量。

每个测试用例包含三行:

第一行包含两个整数n和k(3≤n≤2×10^5;1≤k≤20),分别表示好字符串s的长度和条件的数量。

第二行包含k个整数x1, x2, …, xk(3≤x1
第三行包含k个整数c1, c2, …, ck(3≤c1≤c2≤…≤ck≤min(10^9, (n+1)n/2)),其中ci是第i个条件中的回文子串数量。

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

输出:

对于每个测试用例,如果不存在长度为n的好字符串s满足所有条件,则输出NO。

否则,输出YES和一个长度为n的由小写拉丁字母组成的好字符串s,该字符串满足所有条件。如果存在多个答案,则输出其中任意一个。题目大意:给定一个字符串t,求t中不同回文子串的个数。即使某些子串在t中多次出现,也只计算一次。定义p(t)为字符串t中不同回文子串的个数。例如,字符串t=abcbbcabcb有6个不同的回文子串:a, b, c, bb, bcb和cbbc。定义p(s, m)=p(t),其中t=s[1…m]。换句话说,p(s, m)是以s为前缀的长度为m的字符串中回文子串的个数。例如,p(abcbbcabcb, 5)=p(abcbb)=5。给定一个整数n和k个“条件”(k≤20)。如果一个由n个小写拉丁字母组成的字符串s同时满足所有k个条件,则称该字符串s为“好字符串”。一个条件是一个二元组(xi, ci),含义是:p(s, xi)=ci,即字符串s的前xi个字符包含ci个唯一的回文子串。找到一个好字符串s或报告这样的s不存在。 输入输出数据格式: 输入: 第一行包含一个整数t(1≤t≤10^4),表示测试用例的数量。 每个测试用例包含三行: 第一行包含两个整数n和k(3≤n≤2×10^5;1≤k≤20),分别表示好字符串s的长度和条件的数量。 第二行包含k个整数x1, x2, …, xk(3≤x1

加入题单

上一题 下一题 算法标签: