report VI VI EN
Register | Login
  • HOME
  • PROBLEMSET
  • ROADMAP
  • COMPETITION
  • TOPIC
  • RANKING
  • GUIDE
  • MASHUP
  • ABOUT
  • CONTACT
  • Problem
  • Submit
  • Results
Construct number - FlandreOJ: Flandre Online Judge

Construct number

Time limit: 2000 ms
Memory limit: 256 MB
**Description:** Given two numeric strings $s$ and $t$ of length $n$. You construct a number from $s$ by processing its digits from left to right (from $s[1]$ to $s[n]$). For each digit, you can append it either to the **left** or to the **right** of the number currently formed. **Task**: Find the smallest number $\ge t$ that can be created. If no such number exists, output -1. #### Input - The first line contains $T$ ($1 \le T \le 500$), the number of test cases. - For each test case: - First line contains $n$ ($1 \le n \le 500$). - Second line contains string $s$. - Third line contains string $t$. - It is guaranteed that the sum of $n$ over $T$ test cases does not exceed $500$. #### Output - For each test case, print the result on a new line. #### Example Input: ``` 2 3 123 200 4 4321 1234 ``` Output: ``` 213 1234 ``` #### Subtasks - **Subtask 1 (20 points)**: $n \le 16$. - **Subtask 2 (60 points)**: $n \le 100$. - **Subtask 3 (20 points)**: No additional constraints.
PTNK Team Selection Test [2025 - 2026]
Balanced subarray
Construct number
Orthogonal pairs
Bubble sort
Optimal subset
Water troughs
Topic
2026
Rating 800
Solution (0) Solution