https://codeforces.com/contest/1833/problem/A
时间: 2023-06-21 10:08:30 浏览: 211
这是一道 Codeforces 竞赛中的题目,题目链接为 https://codeforces.com/contest/1833/problem/A 。
题目大意为给定一个字符串 $s$,要求找到一个最短的回文串 $t$,满足 $t$ 是 $s$ 的子串。如果有多个解,输出任意一个即可。
解题思路:
我们可以先判断 $s$ 自身是否是一个回文串,如果是,直接输出 $s$ 即可。如果不是,我们可以从左往右遍历 $s$,找到第一个出现在 $s$ 中的字符 $c$,并将 $c$ 插入到 $s$ 的最左边,得到一个新的字符串 $t$。此时,$t$ 必定是 $s$ 的子串,并且 $t$ 的长度比 $s$ 的长度多 $1$。我们可以继续检查 $t$ 是否是一个回文串,如果是,输出 $t$,否则重复刚才的操作,直到找到一个回文串为止。
代码实现:
相关问题
https://codeforces.com/contest/1810/problem/Bhttps://codeforces.com/contest/1810/problem/B
Problem:
You are given an array a of n integers. You can perform the following operation any number of times: choose two distinct indices i and j (1≤i,j≤n) such that ai≠aj and replace ai with aj and aj with ai.
An array is called beautiful if it is sorted in non-decreasing order (a1≤a2≤…≤an).
Find the minimum number of operations needed to make the given array beautiful.
Solution:
Observation: If we can't make the array beautiful, output -1.
Let's assume we have a beautiful array as b.
If we swap two distinct indices i and j in array a, then b will also have swapped elements b[i] and b[j]. So, we need to find the minimum number of swaps required to convert array a to array b.
We can do this by counting the number of inversions in array a. An inversion is a pair of indices (i,j) such that i<j and ai>aj. The number of inversions in array a is equal to the number of swaps required to sort the array in non-decreasing order.
If the number of inversions is odd, we can't make the array beautiful.
If the number of inversions is even, then we can make the array beautiful. The minimum number of swaps required is equal to the number of inversions.
Time Complexity: O(n log n)
Space Complexity: O(n)
Implementation:
- First, we count the number of inversions in array a using merge sort.
- If the number of inversions is odd, output -1.
- Otherwise, output the number of inversions.
https://codeforces.com/contest/1809/problem/C
The problem statement can be found at Codeforces website.
Approach:
Let's start by looking at some examples:
- 1, 2, 3, 4, 5 → No moves needed.
- 2, 1, 3, 5, 4 → One move needed: swap index 1 and 2.
- 5, 4, 3, 2, 1 → Two moves needed: swap index 1 and 5, then swap index 2 and 4.
We can observe that in order to minimize the number of moves, we need to sort the array in non-descending order and keep track of the number of swaps we make. We can use bubble sort to sort the array and count the number of swaps.
Let's see how bubble sort works:
- Start from the first element, compare it with the second element, and swap them if the second element is smaller.
- Move to the second element, compare it with the third element, and swap them if the third element is smaller.
- Continue this process until the second-to-last element. At this point, the largest element is in the last position.
- Repeat the above process for the remaining elements, but exclude the last position.
In each iteration of the above process, we can count the number of swaps made. Therefore, the total number of swaps needed to sort the array can be obtained by summing up the number of swaps made in each iteration.
Implementation:
We can implement the above approach using a simple bubble sort algorithm. Here's the code:
- First, we read the input array and store it in a vector.
- We define a variable to keep track of the total number of swaps made and set it to 0.
- We run a loop from the first element to the second-to-last element.
- In each iteration of the above loop, we run another loop from the first element to the second-to-last element minus the current iteration index.
- In each iteration of the inner loop, we compare the current element with the next element and swap them if the next element is smaller.
- If a swap is made, we increment the total number of swaps made.
- Finally, we output the total number of swaps made.
Time Complexity:
The time complexity of bubble sort is O(n^2). Therefore, the overall time complexity of the solution is O(n^2).
Space Complexity:
We are using a vector to store the input array. Therefore, the space complexity of the solution is O(n).
Let's see the implementation of the solution.
阅读全文
相关推荐
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)