• B. String Modification


    B. String Modification

    time limit per test

    1 second

    memory limit per test

    256 megabytes

    input

    standard input

    output

    standard output

    Vasya has a string ss of length nn. He decides to make the following modification to the string:

    1. Pick an integer kk, (1≤k≤n1≤k≤n).
    2. For ii from 11 to n−k+1n−k+1, reverse the substring s[i:i+k−1]s[i:i+k−1] of ss. For example, if string ss is qwer and k=2k=2, below is the series of transformations the string goes through:
      • qwer (original string)
      • wqer (after reversing the first substring of length 22)
      • weqr (after reversing the second substring of length 22)
      • werq (after reversing the last substring of length 22)
      Hence, the resulting string after modifying ss with k=2k=2 is werq.

    Vasya wants to choose a kk such that the string obtained after the above-mentioned modification is lexicographically smallest possible among all choices of kk. Among all such kk, he wants to choose the smallest one. Since he is busy attending Felicity 2020, he asks for your help.

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

    • aa is a prefix of bb, but a≠ba≠b;
    • in the first position where aa and bb differ, the string aa has a letter that appears earlier in the alphabet than the corresponding letter in bb.

    Input

    Each test contains multiple test cases.

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

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

    The second line of each test case contains the string ss of nn lowercase latin letters.

    It is guaranteed that the sum of nn over all test cases does not exceed 50005000.

    Output

    For each testcase output two lines:

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

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

    Example

    input

    Copy

    6
    4
    abab
    6
    qwerty
    5
    aaaaa
    6
    alaska
    9
    lfpbavjsm
    1
    p
    

    output

    Copy

    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=1k=1 : abab
    • for k=2k=2 : baba
    • for k=3k=3 : abab
    • for k=4k=4 : baba

      The lexicographically smallest string achievable through modification is abab for k=1k=1 and 33. Smallest value of kk needed to achieve is hence 11

    手写一下样例不难发现,交换k次就是把原字符串的前缀后缀重新拼接,注意交换次数为奇数的时候还需要翻转前缀

    1. # include
    2. # include
    3. # include
    4. using namespace std;
    5. int main ()
    6. {
    7. int t;
    8. cin>>t;
    9. while(t--)
    10. {
    11. int n;
    12. cin>>n;
    13. string s;
    14. cin>>s;
    15. string minn=s;
    16. int ans=1;
    17. string pree="";
    18. pree+=s[0];
    19. int pos=1;
    20. for(int i=1; i
    21. {
    22. string now;
    23. int cnt=n-i;
    24. if((cnt)%2)
    25. {
    26. string bac=s.substr(i);
    27. now=bac+pree;
    28. }
    29. else
    30. {
    31. string pre=s.substr(0,i);
    32. string bac=s.substr(i);
    33. now=bac+pre;
    34. }
    35. pree=s[pos]+pree;
    36. pos++;
    37. if(now
    38. {
    39. ans=i+1;
    40. minn=now;
    41. }
    42. }
    43. cout<
    44. }
    45. return 0;
    46. }

  • 相关阅读:
    c++ 条件变量使用详解 wait_for wait_unitl 虚假唤醒
    NSQ安装与运行
    [C++数据结构](22)哈希表与unordered_set,unordered_map实现
    【设计模式】【第一章】【支付场景】【策略模式 + 工厂模式 + 门面模式 + 单例模式】
    windows环境下tensorflow安装
    基于遥感影像的分类技术(监督/非监督和面向对象的分类技术)
    csdn,是时候说再见!
    OpenCV-直方图反向投影
    P1441 砝码称重 (状压
    自定义修改Discuz X3 今日发帖数量 昨日发帖数量 帖子总数 会员总数
  • 原文地址:https://blog.csdn.net/jisuanji2606414/article/details/126300895