题意:

求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 ;
}

最新文章

  1. CSS3简单动画
  2. 8-IO总结
  3. centos查找未挂载磁盘格式化并挂载
  4. 使用jquery的imagecropper插件做用户头像上传 兼容移动端
  5. 【转】MYSQL入门学习之三:全文本搜索
  6. 用PowerShell批量部署wsp包
  7. 【转】Qt之模型/视图
  8. using和yield return
  9. solr常用命令
  10. vs打开项目,创建虚拟目录,提示权限不足无法写入配置文件
  11. List,Set,Map三种接口的区别
  12. gorm的日志模块源码解析
  13. 使用ethtool显示硬件PHY信息
  14. axios 发送post请求的时候会发送两次
  15. 简易webpack 入门
  16. flutter图片铺满父框
  17. [Python] 04 - os &amp; sys module
  18. topcoder srm 300 div1
  19. JVM内存模型:程序计数器
  20. spring核心容器

热门文章

  1. EF的导航属性
  2. oc和swift对代码的分组,方便代码查找和导航用
  3. Graphics与Canvas
  4. poi的基本导入
  5. nhandled rejection Error: EPERM: operation not permitted, open &#39;C:\Program Files\nodejs\node_cache npm ERR! cb() never called!
  6. es string 分词完整示例
  7. web开发:web前端初识
  8. UCOSII 之 任务统计
  9. 解读Position
  10. VMware虚拟机CentOS与宿主机共享目录