You are given a string ss of length nn and a number kk. Let's denote by rev(s)rev(s) the reversed string ss (i.e. rev(s)=snsn−1...s1rev(s)=snsn−1...s1). You can apply one of the two kinds of operations to the string:
- replace the string ss with s+rev(s)s+rev(s)
- replace the string ss with rev(s)+srev(s)+s
How many different strings can you get as a result of performing exactly kk operations (possibly of different kinds) on the original string ss?
In this statement we denoted the concatenation of strings ss and tt as s+ts+t. In other words, s+t=s1s2...snt1t2...tms+t=s1s2...snt1t2...tm, where nn and mm are the lengths of strings ss and tt respectively.
Input
The first line contains one integer tt (1≤t≤1001≤t≤100) — number of test cases. Next 2⋅t2⋅t lines contain tt test cases:
The first line of a test case contains two integers nn and kk (1≤n≤1001≤n≤100, 0≤k≤10000≤k≤1000) — the length of the string and the number of operations respectively.
The second string of a test case contains one string ss of length nn consisting of lowercase Latin letters.
Output
For each test case, print the answer (that is, the number of different strings that you can get after exactly kk operations) on a separate line.
It can be shown that the answer does not exceed 109109 under the given constraints.
Example
input
Copy
4 3 2 aab 3 3 aab 7 1 abacaba 2 0 ab
output
Copy
2 2 1 1
Note
In the first test case of the example:
After the first operation the string ss can become either aabbaa or baaaab. After the second operation there are 2 possibilities for ss: aabbaaaabbaa and baaaabbaaaab.
思路:这个题是让s=s+rev(s) 或 s=rev(s)+s (res是反转的意思)我们只要将反转的字符串加到前面或后面那么就一定能构成回文字符串,例如:abc,反转加到后面是以最后的字符为对称的:abccba;反转加到前面是以第一个为对称的:cbaabc。如果构成回文,那么再组合最后(最长的)长度的字符串种类不变,因为再反转,回文的字符串依旧是原来的样子,就相当于直接将该字符串加上即可。
引申:如果s=s+s ; s=s+rev(s) ; s=rev(s)+s ; s=rev(s)+rev(s) 。这四种,也就是可以将反转或不反转任意加,那么就可以将反转的加前加后分开讨论。也就是原加原,反加反,的变化会引申入中间的两种。
回文的整体判断:判断整个字符串是否是回文,可以将该字符串反转后直接比较。
完整代码:
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;
typedef long long ll;
typedef vector<int> vi;
//#define int long long
#define fir first
#define sec second
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)x.size()
#define rep(i, l, r) for (int i = l; i <= r; ++i)
#define repd(i, l, r) for (int i = l; i >= r; --i)
#define pb push_back
//#define mint modint<(int)1e9 + 7>
#define defop(type, op) \
inline friend type operator op (type u, const type& v) { return u op ##= v; } \
type& operator op##=(const type& o)
template<int mod>
struct modint {
int x;
// note that there is no correction, simply for speed
modint(int xx = 0): x(xx) {}
modint(ll xx): x(int(xx % mod)) {}
defop(modint, +) {
if ((x += o.x) >= mod) x -= mod;
return *this;
}
defop(modint, -) {
if ((x -= o.x) < 0) x += mod;
return *this;
}
defop(modint, * ) { return *this = modint(1ll * x * o.x); }
modint pow(ll exp) const {
modint ans = 1, base = *this;
for (; exp > 0; exp >>= 1, base *= base)
if (exp & 1) ans *= base;
return ans;
}
defop(modint, /) { return *this *= o.pow(mod - 2); }
};
//using mint = modint<(int)1e9 + 7>;
typedef modint<(int)1e9 + 7> mint;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t,n,k;
cin>>t;
while(t--)
{
cin>>n>>k;
string a,b;
cin>>a;
b=a;
reverse(a.begin(),a.end());
if(a==b or k==0)
{
cout<<"1"<<endl;
}
else
{
cout<<"2"<<endl;
}
}
return 0;
}