1040 Longest Symmetric String (25 分)

最长连续回文子串,正反匹配最长公共连续子串

#include <bits/stdc++.h>
#define LOCAL
using namespace std;

template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { os << '{'; string sep; for (const T &x : v) os << sep << x, sep = ", "; return os << '}'; }
void dbg_out() { cerr << endl; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); }
#ifdef LOCAL
#define dbg(...) cerr << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
#else
#define dbg(...)
#endif

#define vec vector
#define ll long long
#define ld long double
#define sza(x) ((int)x.size())
#define all(a) (a).begin(), (a).end()

const int MAX_N = 1e5 + 5;
const ll MOD = 1e9 + 7;
const ll INF = 1e9;
const ld EPS = 1e-9;

int f[1010][1010];
string s1;

void solve() {
    getline(cin, s1);
    string s2 = s1;
    reverse(all(s2));
    int n = s1.size();
    int ans = 0;
    for(int i = 0; i < n; i ++){
        for(int j = 0; j < n; j ++){
            if(s1[i] == s2[j]){
                f[i][j] = ((i && j) ? f[i - 1][j - 1] : 0) + 1;
                ans = max(f[i][j], ans);
            }
        }
    }
    cout << ans << endl;
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    int tc = 1;
    // cin >> tc;
    for (int t = 1; t <= tc; t++) {
        // cout << "Case #" << t << ": ";
        solve();
    }
}
posted @ 2022-02-05 11:01  yys_c  阅读(24)  评论(0编辑  收藏  举报