• longpo的回文




    #include <bits/stdc++.h>
    using namespace std;
    const int N = 55;
    #define ll long long
    char c[N], o[N], a[N], b[N];
    int n, m, val; ll d[N][N], f[N][N];
    #define chk(a, b) (a = a > b ? b : a)
    signed main() {
        scanf ("%s %d", c + 1, &m);
        n = strlen (c + 1);
        memset (d, 0x1f, sizeof (d));
        for (int i = 0; i <= 26; ++i) d[i][i] = 0;
        for (int i = 1; i <= m; ++i) {
            scanf ("%s", o);
            if (o[0] == 'e') {
                scanf ("%s %d", a, &val);
                chk (d[a[0] - 'a' + 1][0], val);
            } else if (o[0] == 'a') {
                scanf ("%s %d", a, &val);
                chk (d[0][a[0] - 'a' + 1], val);
            } else {
                scanf ("%s %s %d", a, b, &val);
                chk (d[a[0] - 'a' + 1][b[0] - 'a' + 1], val);
            }
        }
        for (int k = 0; k <= 26; ++k)
            for (int i = 0; i <= 26; ++i)
                for (int j = 0; j <= 26; ++j)
                    chk (d[i][j], d[i][k] + d[k][j]);
        memset (f, 0x1f, sizeof (f)); ll inf = f[0][0];
        for (int i = 1; i <= n; ++i) f[i][i] = f[i][i - 1] = 0;
        for (int i = 1; i <= n; ++i) c[i] = c[i] - 'a' + 1;
        for (int len = 2; len <= n; ++len) {
            for (int l = 1; l + len - 1 <= n; ++l) {
                int r = l + len - 1;
                if (c[l] == c[r]) f[l][r] = f[l + 1][r - 1];
                for (int k = 0; k <= 26; ++k) {
                    chk (f[l][r], f[l + 1][r] + d[c[l]][k] + d[0][k]);
                    chk (f[l][r], f[l][r - 1] + d[c[r]][k] + d[0][k]);
                    chk (f[l][r], f[l + 1][r - 1] + d[c[r]][k] + d[c[l]][k]);
                }
            }
        } if (f[1][n] >= inf) f[1][n] = -1;
        return printf ("%lld
    ", f[1][n]), 0;
    }
    
  • 相关阅读:
    Mybatis 接口绑定
    Spring AOP
    Spring 基础使用
    Java 类的生命周期
    Mybatis 测试延迟加载
    Mybatis
    eclipse 常用jar包总结
    Web 过滤器参数设置问题
    Web 单元测试
    zabbix监控-自定义监控与报警(二)
  • 原文地址:https://www.cnblogs.com/whx666/p/13369777.html
Copyright © 2020-2023  润新知