zoukankan      html  css  js  c++  java
  • 洛谷P1481 魔族密码(LIS)

    题意

    题目链接

    给出一堆字符串,若一个串是另一个串的前缀 ,那么它们可以连接在一起

    问最大的链接长度

    Sol

    LIS沙比提其实是做完了才看出是LIS

    #include<cstdio>
    #include<algorithm>
    #include<cstring>
    #include<iostream>
    #define LL long long
    // #define int long long  
    using namespace std;
    const int MAXN = 2001, INF = 1e9 + 7, mod = 998244353;
    inline int read() {
        char c = getchar(); int x = 0, f = 1;
        while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
        while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
        return x * f;
    }
    int N;
    string s[MAXN];
    int f[MAXN];
    bool suf(string a, string b) {
        int cur = 0;
        for(int i = 0; i < a.length(); i++) {
            if(a[i] != b[cur]) return 0;
            cur++;
        }
        return 1;
    }
    main() {
        cin >> N;
        for(int i = 1; i <= N; i++) cin >> s[i];
        int ans = 0;
        for(int i = 1; i <= N; i++) {
            f[i] = 1;
            for(int j = 1; j < i; j++) {
                if(suf(s[j], s[i])) f[i] = max(f[i], f[j] + 1);
            }
            ans = max(ans, f[i]);
        }
        printf("%d", ans);
        return 0;
    }
    /*
    */
  • 相关阅读:
    [专题六] 位运算
    [专题五] 二叉树
    [专题四] 并查集
    [专题三] 图论
    [专题二] 排序
    [专题一] 栈和队列
    我的最新书单
    虚拟机极简配置manjaro gnome
    运算符重载
    Manjaro kde 18.0安装与基本配置
  • 原文地址:https://www.cnblogs.com/zwfymqz/p/9627501.html
Copyright © 2011-2022 走看看