题目大意
给你一个字符串,你遍历这个字符串,每次从这个字符串的第一个摘下一个字符插入到一个新字符串中,你使得新字符串的字典序最小,问你有多少种不同方法得到字典序最小的新字符串。
第一种插入方法是插入到新字符串的前面,第二种是插入到新字符串的后面。
解题思路
发现只有前面连续的相同字符才有两种插入方法,后面就只能有一种插入方法了,所以我们看前面有多少个相同连续的字符,那么答案就是 2 2 2 的这么多次幂。
Code
#include <bits/stdc++.h>
#define ll long long
#define qc ios::sync_with_stdio(false); cin.tie(0);cout.tie(0)
#define fi first
#define se second
#define PII pair<int, int>
#define PLL pair<ll, ll>
#define pb push_back
using namespace std;
const int MAXN = 2e5 + 7;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const ll mod = 1e9 + 7;
inline int read()
{
int x=0,f=1;char ch=getchar();
while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}
while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;
}
int n;
string s;
void solve(){
cin >> n >> s;
if(n == 1){
cout << "1\n";
return ;
}
ll ans = 1;
char l = s[0];
for(int i = 1; i < n; i++){
if(s[i] == l){
ans *= 2;
ans %= mod;
}
else
break;
}
cout << ans % mod << endl;
}
int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
qc;
int T;
cin >> T;
// T = 1;
while(T--){
solve();
}
return 0;
}