#include<bits/stdc++.h> using namespace std; #define maxn 1000005 int n; string s; int jmin,f[1000],t[1000]; bool kt(int i,int j) { for(int k=i;k<=(i+j)/2+1;k++) { // cout<<i<<" "<<j<<endl; if(s[k]!=s[j+i-k]) return false; } return true; } void truy_vet() { string r="",a[300]; int d=0; while(f[n]>0) { r=""; for(int i=t[n]+1;i<=n;i++) r=r+s[i]; d++; a[d]=r; // cout<<n<<endl; n=t[n]; } for(int i=d;i>=1;i--) cout<<a[i]<<endl; } void giai() { f[0]=0;f[1]=1; for(int i=2;i<=n;i++) { // cout<<"i="<<i<<endl; jmin=i-1; // cout<<f[jmin]<<endl; for(int j=0;j<=i-1;j++) { if(kt(j+1,i)==true&&f[jmin]>f[j]) { // cout<<(j+1)<<" "<<i<<endl; jmin=j; // t[i]=j; } f[i]=f[jmin]+1; t[i]=jmin; } } // for(int i=1;i<=n;i++) cout<<f[i]<<" "; // cout<<endl; cout<<f[n]<<endl; truy_vet(); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); freopen("palindr.inp", "r", stdin); freopen("palindr.out", "w", stdout); cin>>s; n=s.length(); s=' '+s; giai(); }
* Chuyên dạy lập trình ONLINE cho học sinh THCS, THPT *.
Mọi giao lưu, trao đổi, xin liên hệ: Lê Quang Vinh - zalo: 037.803.8755.
Page: Lớp học Code Sky
Group FB1: Ôn thi HSG9 - THTB - TS10 chuyên tin
Group FB2: Học Scratch - Ôn thi Tin học trẻ bảng A
Chủ Nhật, 24 tháng 4, 2022
PALINDR c++
Đăng ký:
Đăng Nhận xét (Atom)
Không có nhận xét nào:
Đăng nhận xét
Lưu ý: Chỉ thành viên của blog này mới được đăng nhận xét.