A Prefix and Suffix Array
解题思路:
如果字符串是回文,那么相同长度的前后缀应该是反转后向等的
参考代码:
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 2e5 + 5;
ll T;
ll n;
string s;
vector<string>v[100];
void solve() {
cin >> n;
for (int i = 1; i < n; i++) v[i].clear();
for (int i = 1; i <= 2 * n - 2; i++) {
cin >> s;
v[s.size()].push_back(s);
}
for (int i = 1; i < n; i++) {
reverse(v[i][1].begin(), v[i][1].end());
if (v[i][0] != v[i][1]) {
cout << "NO" << endl;
return;
}
}
cout << "YES" << endl;
}
int main() {
cin >> T;
while (T--) solve();
return 0;
}
B Not Dividing
解题思路:
如果,那么让
即可(除了1以外,所以得提前把1预处理一下)
参考代码:
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 1e4 + 5;
ll T;
ll n;
ll a[N];
void solve() {
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
if (a[i] == 1) a[i]++;
}
for (int i = 1; i < n; i++) {
if (a[i + 1] % a[i] == 0) a[i + 1]++;
}
for (int i = 1; i <= n; i++) cout << a[i] << ' ';
cout << endl;
}
int main() {
cin >> T;
while (T--) solve();
return 0;
}
C Scoring Subsequences
解题思路:
第一个数肯定是1,对于题目给的式子,有一个方法可以构造,因为题目给的是递增的序列,可以往前找第一个贡献小于1的数的下标,可以用二分(标答),我莫名其妙过了
参考代码:
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 2e5 + 5;
ll T;
ll n;
int a[N];
void solve() {
int ans[N] = {};
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1; i <= n; i++) {
ans[i + a[i] - 1] = max(a[i], ans[i]);
}
for (int i = 1; i <= 2 * n; i++) {
if (ans[i] > 0) {
int t = ans[i];
for (int j = i; j >= 1 && t; j--) {
if (ans[j] <= t) ans[j] = t--;
else break;
}
}
}
for (int i = 1; i <= n; i++) cout << ans[i] << ' ';
cout << endl;
}
int main() {
cin >> T;
while (T--) solve();
return 0;
}
博客围绕三道算法题展开,包括A Prefix and Suffix Array、B Not Dividing、C Scoring Subsequences。分别给出了解题思路,如A题判断回文时前后缀反转相等,C题可通过二分找第一个贡献小于1的数的下标等,还给出了参考代码。

被折叠的 条评论
为什么被折叠?



