zoukankan      html  css  js  c++  java
  • [LeetCode] 522. Longest Uncommon Subsequence II


    Given a list of strings, you need to find the longest uncommon subsequence among them. The longest uncommon subsequence is defined as the longest subsequence of one of these strings and this subsequence should not be any subsequence of the other strings.

    A subsequence is a sequence that can be derived from one sequence by deleting some characters without changing the order of the remaining elements. Trivially, any string is a subsequence of itself and an empty string is a subsequence of any string.

    The input will be a list of strings, and the output needs to be the length of the longest uncommon subsequence. If the longest uncommon subsequence doesn't exist, return -1.

    Example 1:

    Input: "aba", "cdc", "eae"
    Output: 3
    

    Note:

    1. All the given strings' lengths will not exceed 10.
    2. The length of the given list will be in the range of [2, 50].

    题意:找出一个字符串数组中最长的特殊字符串

    特殊字符串指的是,它不是任何非它的子序列串,子序列串就不解释了

    其一,如果一个字符串的长度大于另一个字符串,那么它一定不是另一个的子序列串,

    其二,我们找到最长的那个可以从最长的开始判断。

    所以综上两点,我们可以想到对其先排序,从大到小按照字符串的长度,但是有个问题,如果是等长的,那么所有和它等长的我们都需要去判断,

    所以我们需要维护一个数组,其中包含了需要和当前元素判断的数组

    class Solution {
        private boolean ifSubString(String parent, String sub) {
            int i = 0;
            int j = 0;
            while (i < parent.length() && j < sub.length()) {
                if (parent.charAt(i) == sub.charAt(j)) {
                    i++;
                    j++;
                } else
                    i++;
            }
            if (j >= sub.length())
                return true;
            return false;
        }
        public int findLUSlength(String[] strs) {
            if (strs.length == 0)
                return 0;
            Map<Integer, ArrayList<String>> map = new TreeMap<>(new Comparator<Integer>() {
                @Override
                public int compare(Integer o1, Integer o2) {
                    if (o1.intValue() > o2.intValue())
                        return -1;
                    if (o1.intValue() < o2.intValue())
                        return 1;
                    return 0;
                }
            });
            for (int i = 0; i < strs.length; i++) {
                int lth = strs[i].length();
                if (map.containsKey(lth)) {
                    ArrayList list = map.get(lth);
                    list.add(strs[i]);
                    map.put(lth, list);
                } else {
                    ArrayList<String> list = new ArrayList<>();
                    list.add(strs[i]);
                    map.put(lth, list);
                }
            }
    
            ArrayList<String> res = new ArrayList<>();
            for (Map.Entry entry : map.entrySet()) {
                ArrayList<String>list = (ArrayList) entry.getValue();
                for (int i = 0; i < list.size(); i++)
                    res.add(list.get(i));
    
                if (res.size() == 1)
                    return res.get(0).length();
    
                for (int i = 0; i < res.size(); i++) {
                    boolean flag = true;
                    for (int j = 0; j < res.size(); j++) {
                        if (i == j)
                            continue;
                        if (ifSubString(res.get(j), res.get(i))) {
                            flag = false;
                            break;
                        }
                    }
                    if (flag)
                        return (int) entry.getKey();
                }
            }
            return -1;
        }
    }
  • 相关阅读:
    洛谷 P1591 阶乘数码
    洛谷 P2008 大朋友的数字
    洛谷 P1716 双调序列
    洛谷 P2309 loidc,卖卖萌
    洛谷 P1324 矩形分割
    洛谷 P2690 接苹果
    洛谷 P1239 计数器
    hdu_4352_XHXJ's LIS(数位DP+状态压缩)
    hdu_5648_DZY Loves Math
    hdu_5179_beautiful number(数位DP)
  • 原文地址:https://www.cnblogs.com/Moriarty-cx/p/9911522.html
Copyright © 2011-2022 走看看