字符串暴力枚举子序列求LCS
2024-08-28 07:29:11
题意:
求n个串里的LCS,长度相同时按照字典序排序
solution:
断环为链,二进制枚举子序列,压入vector,按照字典序排序
把出现次数为n的,压入第二个vector
输出最长的第二个vector里最长的序列
#include<bits/stdc++.h>
#define endl '\n'
using namespace std;
bool cmp(string a,string b)
{
return a.size()<b.size();
}
int main() {
// ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
int n;
while(cin >> n) {
string s;
vector<string>a;
if(n == ) {
cin >> s;
int sz= s.size();
vector<string>xx;
s+=s;
// cout << s << endl;
for(int i = ; i < sz;i++) xx.push_back(s.substr(i,sz));//截取起点为i长度为sz的字串
sort(xx.begin(),xx.end());
for(int i = ; i < xx.size(); i++) cout <<xx[i]<<endl;
cout << xx[] << endl;continue;
} for(int i = ; i < n; i++) {
cin >> s;
int sz = s.size();
s += s;
vector<string>b;
for(int j = ; j < sz; j++){//断环为链
for(int k = ; k < ( << sz); k++) {//用枚举子集来表示序列
string t;
for(int p = ; p < sz; p++) {
if(k & ( << p)) {
t += s[j + p];
}
}
//cout << t << endl;
b.push_back(t);
}
//cout << endl;
}
sort(b.begin(),b.end());
b.erase(unique(b.begin(),b.end()),b.end());//每一个串的子序列按照字典序排序并去重
for(int j = ; j < b.size(); j++) {//把每一个串的子序列加入总的字符串里
a.push_back(b[j]);
}
}
sort(a.begin(),a.end());//按照所有串的子序列
// for(int i = 0; i < a.size(); i++) cout << a[i] <<endl;
int ct = , maxx = ;
vector<string>c,d;
for(int i = ; i < a.size(); i++) {
if(a[i] == a[i- ]) ct++;
else ct = ;
if(ct == n) c.push_back(a[i]),maxx=max(maxx,(int)a[i].size());//n个串里的公共子序列
}
// for(int i = 0; i < c.size(); i++) cout << c[i] <<endl;
if(c.size() == ) {
cout << << endl; continue;
}
for(int i = ; i < c.size(); i++) {
if(c[i].size() == maxx){
cout<<c[i]<<endl;break;
}
}
}
return ;
}
最新文章
- CSS3简单动画
- 8-IO总结
- centos查找未挂载磁盘格式化并挂载
- 使用jquery的imagecropper插件做用户头像上传 兼容移动端
- 【转】MYSQL入门学习之三:全文本搜索
- 用PowerShell批量部署wsp包
- 【转】Qt之模型/视图
- using和yield return
- solr常用命令
- vs打开项目,创建虚拟目录,提示权限不足无法写入配置文件
- List,Set,Map三种接口的区别
- gorm的日志模块源码解析
- 使用ethtool显示硬件PHY信息
- axios 发送post请求的时候会发送两次
- 简易webpack 入门
- flutter图片铺满父框
- [Python] 04 - os &; sys module
- topcoder srm 300 div1
- JVM内存模型:程序计数器
- spring核心容器
热门文章
- EF的导航属性
- oc和swift对代码的分组,方便代码查找和导航用
- Graphics与Canvas
- poi的基本导入
- nhandled rejection Error: EPERM: operation not permitted, open &#39;C:\Program Files\nodejs\node_cache npm ERR! cb() never called!
- es string 分词完整示例
- web开发:web前端初识
- UCOSII 之 任务统计
- 解读Position
- VMware虚拟机CentOS与宿主机共享目录