7.22 第二场 I love string

I love string

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 290 Accepted Submission(s): 166

Problem Description

Mr X likes to play string games.

Mr X has an operation sequence. This operation sequence can be written as a string. For each operation, the next character of the operation sequence can be inserted before or after the current string. For example, my operation sequence is “aabac”, suppose the sequence obtained after the first four operations is “baaa”, then after the last operation, the string may become “baaac” or “cbaaa”. It can be seen that there is only one operation method for the first operation. For other operations, there are only two methods of operation.

For each operation method, there will be a score. The smaller the lexicographic order of the final string, the higher the final score.

Then, for a given operation sequence, how many operation methods can get the maximum score.

The two operation methods are different. If and only if there is a certain operation (not the first operation), one operation will be inserted before the current string, and the other operation will be inserted after the current string.

Input

Enter a positive integer T (T≤10) on the first line to represent the number of test cases.

For each test case:

the first line contains a integer n (1≤n≤100000) to represent the length of the string.

the second line contains a string of lowercase letters , which represents the sequence of operations.

Output

For each test case, output a line of a positive integer to represent the number of schemes, and the answer is modulo 1000000007

Sample Input

1
5
abcde

Sample Output

1

大概题意

一行字符,有头插和尾插两种操作插入结果字符串中,要使结果字符串字典序最小,求操作种数。

思路

当结果字符串中只有同种字符时,头插尾插结果相同,而在保持字典序最小的过程中,当且仅当结果字符串第一个字符与最后一个字符相同时,结果字符串中只有同种字符

代码

//
// Created by Black on 2021/7/22.
//
#include <iostream>
#include <cstring>
#include <vector>

using namespace std;
const int N = 100010;
const int MOD = 1000000007;
int T, n;
char s[N];

int main() {
    cin >> T;
    while (T--) {
        vector<char> c;
        int res = 1;
        cin >> n;
        cin >> s;
        for (int i = 0; i < n; ++i) {
            if (c.empty())c.push_back(s[i]);
            else {
                if (c[0] >= s[i]) {
                    c.insert(c.begin(), s[i]);
                } else {
                    c.push_back(s[i]);
                }
                if (c[0] == c[c.size() - 1]) {
                    res *= 2;
                    res = res % MOD;
                }
            }
        }
        cout << res << endl;

    }
    return 0;
}

posted @   嘿,抬头!  阅读(29)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探
· 为什么 退出登录 或 修改密码 无法使 token 失效
点击右上角即可分享
微信分享提示