【题目链接】: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;
}
};

最新文章

  1. 时间戳TimeStamp处理
  2. 解决xcode8模拟器不能删除应用的问题
  3. 多语言配置--LogisticsPlatform物流平台系统
  4. BZOJ 4003 【JLOI2015】城池攻占
  5. C#入门经典第五版之变量的更多内容编码题训练
  6. UItableview里面的header、footer
  7. mysql服务启动
  8. [算法题] Remove Duplicates from Sorted Array ii
  9. Nginx-动态添加模块
  10. 如何清理Docker占用的磁盘空间?
  11. JavaScript基础知识点总结
  12. 如何在Windows命令行(DOS界面)中调用 编译器 来编译C/C++源程序
  13. 激活win10专业版
  14. Linux驱动总结3- unlocked_ioctl和堵塞(waitqueue)读写函数的实现 【转】
  15. android--------自定义控件 之 ViewGroup
  16. ubuntu 14.04(desktop amd 64) nginx 安装启动停止
  17. NYOJ 252 01串 普通dp
  18. 数据库-mysql储存过程
  19. Getting Started(入门)
  20. 使用python 模仿mybinlog 命令 二进制分析mysql binlog

热门文章

  1. 05、ListActivity的使用
  2. roundabout旋转幻灯
  3. [Swift通天遁地]八、媒体与动画-(6)使用开源类库快速实现滑入动画
  4. promise 小抄
  5. ASP.NET MVC5 之 Log4Net 的学习和使用
  6. Hadoop Hive概念学习系列之hive的脚本执行(二十)
  7. maven 纯注解一步一步搭建Spring Mvc项目(入门)
  8. SQL练习题_用户购买收藏记录合并(拼多多)
  9. 影响ERP成功实施的因素及实施方法
  10. 移动web——bootstrap如何修改原组件