【题目链接】:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/longest-uncommon-subsequence-ii/

【题意】



字符串变成多个了;

(不止两个)

让你求最长不公共子序列

【题解】



因为最大长度为10;

所以把每个长度有哪些字符串记录下来;

然后从字符串长度由大到小枚举len;

假设这个答案序列为len长度的某个字符串;

然后看看len长度的字符串有没有和它一样的字符串(即出现两次及以上)

有的话不行,找另外一个长度为len的字符串;

否则

再看看这个字符串是不是长度比len长的字符串的子串;

如果不是的话;

就表示找到了;

直接输出len;

否则继续找长度为len的另外的字符串;

判断一个字符串是不是另外一个字符串的子串其实很简单的;

O(l1+l2)就能判断出来;



【完整代码】

#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define ps push_back
#define fi first
#define se second
#define rei(x) scanf("%d",&x)
#define rel(x) scanf("%lld",&x)
#define ref(x) scanf("%lf",&x) typedef pair<int, int> pii;
typedef pair<LL, LL> pll; const int dx[9] = { 0,1,-1,0,0,-1,-1,1,1 };
const int dy[9] = { 0,0,0,-1,1,-1,1,-1,1 };
const double pi = acos(-1.0);
const int N = 250; vector <string> v[12]; bool is(string a, string b)
{
int len1 = a.size(), len2 = b.size();
int i = 0, j = 0;
rep1(j,0,len2-1)
if (b[j] == a[i])
{
i++;
if (i == len1)
return true;
}
return false;
} class Solution {
public:
int findLUSlength(vector<string>& strs) {
int n = strs.size();
rep1(i, 0, 10)
v[i].clear();
rep1(i, 0, n-1)
{
int d = strs[i].size();
v[d].ps(strs[i]);
}
rep2(i, 10, 0)
{
int len = v[i].size();
rep1(j, 0, len - 1)
{
bool ok = true;
rep1(k,0,len-1)
if (k != j && v[i][j] == v[i][k])
{
ok = false;
break;
}
if (!ok) continue;
//看看是不是更长串的子串
rep1(k, i + 1, 10)
{
int len2 = v[k].size();
rep1(kk, 0, len2 - 1)
{
if (is(v[i][j], v[k][kk]))
{
ok = false;
break;
}
}
if (!ok) break;
}
if (ok)
return i;
}
}
return -1;
}
};

【LeetCode Weekly Contest 26 Q2】Longest Uncommon Subsequence II的更多相关文章

  1. 【LeetCode Weekly Contest 26 Q1】Longest Uncommon Subsequence I

    [题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/longest-uncommon-subsequence ...

  2. 【LeetCode Weekly Contest 26 Q4】Split Array with Equal Sum

    [题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/split-array-with-equal-sum/ ...

  3. 【LeetCode Weekly Contest 26 Q3】Friend Circles

    [题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/friend-circles/ [题意] 告诉你任意两个 ...

  4. 【LeetCode】522. Longest Uncommon Subsequence II 解题报告(Python)

    [LeetCode]522. Longest Uncommon Subsequence II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemin ...

  5. LeetCode Weekly Contest 26

    写的有点晚了. 我每次都是先看一下这里http://bookshadow.com/leetcode/的思路,然后再开始写我自己的. 1. 521. Longest Uncommon Subsequen ...

  6. [LeetCode] Longest Uncommon Subsequence II 最长非共同子序列之二

    Given a list of strings, you need to find the longest uncommon subsequence among them. The longest u ...

  7. LeetCode Longest Uncommon Subsequence II

    原题链接在这里:https://leetcode.com/problems/longest-uncommon-subsequence-ii/#/description 题目: Given a list ...

  8. [Swift]LeetCode522. 最长特殊序列 II | Longest Uncommon Subsequence II

    Given a list of strings, you need to find the longest uncommon subsequence among them. The longest u ...

  9. 522. Longest Uncommon Subsequence II

    Given a list of strings, you need to find the longest uncommon subsequence among them. The longest u ...

随机推荐

  1. innerxml and outerxml

    xml文件如下 <root> <a></a> <b></b> <c></c> <a></a> ...

  2. Silverlight 2学习笔记二:三个基本布局控件(Canvas、StackPanel、Grid )

    这篇文章主要是翻译了ScottGu博客的文章:Silverlight Tutorial Part 2: Using Layout Management.虽然是翻译,但通过笔记记录,我发现对这三个布局控 ...

  3. CodeForces - 557D Vitaly and Cycle(二分图)

    Vitaly and Cycle time limit per test 1 second memory limit per test 256 megabytes input standard inp ...

  4. Poj2054 color a tree && [HNOI/AHOI2018]排列

    https://zybuluo.com/ysner/note/1120723 题面 原题 某省选强化题 大致意思是给你一颗树,选父亲后才能选儿子. 每个点对答案的贡献为你在第几次选这个点 × 该点权值 ...

  5. 收集主机OS相关数据

    #!/usr/bin/ksh touch hostinfo$(date +%Y%m%d).csv filename=hostinfo$(date +%Y%m%d).csv >${filename ...

  6. Spark SQL中 RDD 转换到 DataFrame

    1.people.txtsoyo8, 35小周, 30小华, 19soyo,882./** * Created by soyo on 17-10-10. * 利用反射机制推断RDD模式 */impor ...

  7. [Swift通天遁地]五、高级扩展-(12)扩展故事板中的元件添加本地化功能

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  8. 关于DOM操作的相关案例

    1.模态框案例 需求: 打开网页时有一个普通的按钮,点击当前按钮显示一个背景图,中心并弹出一个弹出框,点击X的时候会关闭当前的模态框 代码如下: <!DOCTYPE html> <h ...

  9. C# 自己用到的几个参数转换方法

    /// <summary> /// Method:CommandHelper /// Author:Liuyangyi /// Data:2016-05-10 /// </summa ...

  10. html5——地理位置

    获取地理位置 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UT ...