题目 | (∀x∀)
AtCoder Beginner Contest 242
E - (∀x∀)
https://atcoder.jp/contests/abc242/tasks/abc242_e
Time Limit: 2 sec / Memory Limit: 1024 MB
Score :
Problem Statement
Solve the following problem for
Given an integer
is a string of length consisting of uppercase English letters. is a palindrome. in lexicographical order.- That is,
or is lexicographically smaller than .
- That is,
Constraints
is an integer between and (inclusive).- In a single input, the sum of
over the test cases is at most . is a string of length consisting of uppercase English letters.
Input
Input is given from Standard Input in the following format:
Here,
Each test case is in the following format:
Output
Print
Sample Input 1
5
3
AXA
6
ABCZAZ
30
QWERTYUIOPASDFGHJKLZXCVBNMQWER
28
JVIISNEOXHSNEAAENSHXOENSIIVJ
31
KVOHEEMSOZZASHENDIGOJRTJVMVSDWW
Sample Output 1
24
29
212370247
36523399
231364016
This input contains five test cases.
Test case #1:
The AAA
ABA
ACA
AXA
.
Test case #2:
Test case #3:
Be sure to find the count modulo
我的笔记
若回文串
前半截的字典序就已经比 小了,那么可以直接得出结论 。- 如:
和 ,回文串前半截 已经比 小了,因此 。
- 如:
若回文串
前半截等于 的前半截,那么需要比对后半截才能知道字典序的情况。- 如:
和 ,不比对这俩的后半截是没法知道字典序情况的。
- 如:
若回文串
前半截的字典序就已经比 大了,那么可以直接得出结论 ,即不符合该题要求。- 如:
和 ,回文串前半截 已经比 大了,因此 。
- 如:
依靠上面第一条性质,可以先求出
例如字符串
将这些情况数目加起来,便是
那么还有一种情况有可能成立,不能忽略。就是上面第二条性质,回文串
代码
#include <bits/stdc++.h>
#define MOD 998244353
using namespace std;
int main(void)
{
int T;
cin >> T;
while (T--)
{
int N;
string S;
cin >> N >> S;
long long ans = 0, p = 1;
for (int i = (N - 1) / 2; i >= 0; i--)
{
ans += p * (S[i] - 'A');
ans %= MOD;
p *= 26;
p %= MOD;
}
string palindrome = S;
for (int i = 0, j = N - 1; i <= j; i++, j--)
palindrome[j] = palindrome[i];
if (palindrome <= S)
{
ans++;
ans %= MOD;
}
cout << ans << endl;
}
return 0;
}
本文采用 CC BY-SA 4.0 许可,本文 Markdown 源码:Haotian-BiJi