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.
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.
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
思路
:由题意知,当序列字典序最小时,值最大。要求方案数,可知只有当前面序列的所有字母相同时且此字母与前面字母相同时,无论是插在前面还是后面,都满足最优。所以等价于求序列前面连续的等于首字母的个数。
代码如下:
#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
typedef long long ll;
ll qk(ll a,ll b){//快速幂模板
ll ans=1;
while(b){
if(b&1) ans=a*ans%MOD;
a=a*a%MOD;
b>>=1;
}
return ans;
}
int main(){
int t;
cin>>t;
while(t--){
int n;
cin>>n;
string s;
cin>>s;
ll num=0;//记录连续的与首字母相同的字母个数
for(int i=1;i<n;i++){
if(s[i]!=s[0]){
break;
}
if(s[i]==s[0]) num++;
}
cout<<qk(2,num)<<endl;
}
return 0;
}
using namespace std;
#define MOD 1000000007
typedef long long ll;
ll qk(ll a,ll b){//快速幂模板
ll ans=1;
while(b){
if(b&1) ans=a*ans%MOD;
a=a*a%MOD;
b>>=1;
}
return ans;
}
int main(){
int t;
cin>>t;
while(t--){
int n;
cin>>n;
string s;
cin>>s;
ll num=0;//记录连续的与首字母相同的字母个数
for(int i=1;i<n;i++){
if(s[i]!=s[0]){
break;
}
if(s[i]==s[0]) num++;
}
cout<<qk(2,num)<<endl;
}
return 0;
}