日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Codeforce-CodeCraft-20 (Div. 2)-B. String Modification (找规律+模拟)

發(fā)布時(shí)間:2023/12/15 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Codeforce-CodeCraft-20 (Div. 2)-B. String Modification (找规律+模拟) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

Vasya has a string s of length n. He decides to make the following modification to the string:

Pick an integer k, (1≤k≤n).
For i from 1 to n?k+1, reverse the substring s[i:i+k?1] of s. For example, if string s is qwer and k=2, below is the series of transformations the string goes through:
qwer (original string)
wqer (after reversing the first substring of length 2)
weqr (after reversing the second substring of length 2)
werq (after reversing the last substring of length 2)
Hence, the resulting string after modifying s with k=2 is werq.
Vasya wants to choose a k such that the string obtained after the above-mentioned modification is lexicographically smallest possible among all choices of k. Among all such k, he wants to choose the smallest one. Since he is busy attending Felicity 2020, he asks for your help.

A string a is lexicographically smaller than a string b if and only if one of the following holds:

a is a prefix of b, but a≠b;
in the first position where a and b differ, the string a has a letter that appears earlier in the alphabet than the corresponding letter in b.
Input
Each test contains multiple test cases.

The first line contains the number of test cases t (1≤t≤5000). The description of the test cases follows.

The first line of each test case contains a single integer n (1≤n≤5000) — the length of the string s.

The second line of each test case contains the string s of n lowercase latin letters.

It is guaranteed that the sum of n over all test cases does not exceed 5000.

Output
For each testcase output two lines:

In the first line output the lexicographically smallest string s′ achievable after the above-mentioned modification.

In the second line output the appropriate value of k (1≤k≤n) that you chose for performing the modification. If there are multiple values of k that give the lexicographically smallest string, output the smallest value of k among them.

Example
inputCopy
6
4
abab
6
qwerty
5
aaaaa
6
alaska
9
lfpbavjsm
1
p
outputCopy
abab
1
ertyqw
3
aaaaa
1
aksala
6
avjsmbpfl
5
p
1
Note
In the first testcase of the first sample, the string modification results for the sample abab are as follows :

for k=1 : abab
for k=2 : baba
for k=3 : abab
for k=4 : baba
The lexicographically smallest string achievable through modification is abab for k=1 and 3. Smallest value of k needed to achieve is hence 1.
//規(guī)律從后往前數(shù),分奇偶,然后要么是直接放后面,要么是導(dǎo)致,直接寫string模擬這過程完事了。

#include <bits/stdc++.h> using namespace std; template <typename t> void read(t &x) {char ch = getchar();x = 0;t f = 1;while (ch < '0' || ch > '9')f = (ch == '-' ? -1 : f), ch = getchar();while (ch >= '0' && ch <= '9')x = x * 10 + ch - '0', ch = getchar();x *= f; }#define wi(n) printf("%d ", n) #define wl(n) printf("%lld ", n) #define rep(m, n, i) for (int i = m; i < n; ++i) #define rrep(m, n, i) for (int i = m; i > n; --i) #define P puts(" ") typedef long long ll; #define MOD 1000000007 #define mp(a, b) make_pair(a, b) #define N 200005 #define fil(a, n) rep(0, n, i) read(a[i]) //---------------https://lunatic.blog.csdn.net/-------------------// int n; string c;int main() {int t, f;read(t);while (t--){read(n);cin >> c;f=0;string ans = c, s;for (int i = 1; i < n; i++){if ((n - i) % 2 == 0)s = c.substr(i, n - i) + c.substr(0, i );else{string tem = c.substr(0, i );reverse(tem.begin(), tem.end());s = c.substr(i, n - i) +tem;}if (ans > s){f = i;ans = s;}}cout << ans << endl;wi(f + 1), P;} }

總結(jié)

以上是生活随笔為你收集整理的Codeforce-CodeCraft-20 (Div. 2)-B. String Modification (找规律+模拟)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。