Kick Start Round C 2021 : Smaller Strings Problem A
Smaller Strings (6pts, 9pts)
OFFICIAL PROBLEM LINK
https://codingcompetitions.withgoogle.com/kickstart/round/0000000000435c44/00000000007ebe5e
Smaller Strings Solution in C++ :
using namespace std;
const int MOD = (int) 1e9 + 7;
string S, R;
long long P[27][100005];
int main() {
ios::sync_with_stdio(0);
cin.tie(NULL), cout.tie(NULL);
int tests;
cin >> tests;
for (int i=1; i<=26; i++)
{
P[i][0] = 1;
for (int j=1; j<=100000; j++)
P[i][j] = P[i][j-1] * i % MOD;
}
for (int test=1; test<=tests; test++)
{
cout << "Case #" << test << ": ";
int n, k;
cin >> n >> k >> S;
R = S;
long long ans = 0;
for (int i=0, j=n-1; i<=j; i++, j--)
{
int x = S[i] - 'a';
int rem = max(0, n - (i + 1) * 2);
R[j] = S[i];
ans += 1LL * x * P[k][(rem + 1) / 2] % MOD;
}
ans += (R < S);
cout << ans % MOD << "\n";
}
return 0;
}