Hãy luôn nhớ cảm ơn và vote 5*
nếu câu trả lời hữu ích nhé!
#include <bits/stdc++.h>
using namespace std;
bool ktra(const string& s, int a, int b) {
while (a < b) {
if (s[a] != s[b]) {
return false;
}
a++;
b--;
}
return true;
}
int main() {
long long n , ans = 1000000001 ;
cin >> n;
string s;
cin >> s;
long long a[n + 1] , b[n + 1] , c = n , res[n] , cnt = 0;
a[0] = 0;
for (int i = 1; i <= n; ++i) {
a[i] = ans;
for (int j = 0; j < i; ++j) {
if (ktra(s, j, i - 1) && a[j] != ans) {
if (a[i] > a[j] + 1) {
a[i] = a[j] + 1;
b[i] = j + 1;
}
}
}
}
cout << a[n] << endl;
while (c > 0) {
res[cnt++] = c;
c = b[c] - 1;
}
for (int i = cnt - 1; i >= 0; --i) {
cout << res[i] << endl;
}
return 0;
}
Hãy giúp mọi người biết câu trả lời này thế nào?
Xem thêm:
Bảng tin