HDU 1238 Substrings (最长公共子串+DFS)
来源:互联网 发布:mysql破解教程 编辑:程序博客网 时间:2024/05/22 13:54
Substrings
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 9246 Accepted Submission(s): 4388
Problem Description
You are given a number of case-sensitive strings of alphabetic characters, find the largest string X, such that either X, or its inverse can be found as a substring of any of the given strings.
Input
The first line of the input file contains a single integer t (1 <= t <= 10), the number of test cases, followed by the input data for each test case. The first line of each test case contains a single integer n (1 <= n <= 100), the number of given strings, followed by n lines, each representing one string of minimum length 1 and maximum length 100. There is no extra white space before and after a string.
Output
There should be one line per test case containing the length of the largest string found.
Sample Input
23ABCDBCDFFBRCD2roseorchid
Sample Output
22
Author
Asia 2002, Tehran (Iran), Preliminary
题解:找最长的公共子串,反串的子串也算。注意:子串是连续的,子序列是不用连续的。
AC代码:DFS
#include <iostream>#include<stdio.h>#include <algorithm>using namespace std;string str[110]; int t;int cmp(const string& a, const string& b){return a.length()<b.length();}//string substr(int pos = 0,int n = npos) const; 返回从pos开始的n个字符组成的字符串int dfs(string now,string reve,int len){for(int i=1;i<t;i++){int sign=0;for(int j=0;j+len<=str[i].length();j++){string sub=str[i].substr(j,len); //返回从j开始 ,长度为len的子串if(sub==now||sub==reve) {sign=1;break; //找到就继续找}}if(!sign)return 0; //否则直接退出}return len;}int main(){int n;cin>>n;while(n--){cin>>t;for(int i=0;i<t;i++)cin>>str[i];sort(str,str+t,cmp);int len=str[0].length();int max=0;for(int i=len;i>=1;i--){for(int j=0;j+i<=len;j++){string now=str[0].substr(j,i); //回从 j开始的 i个字符组成的字符串string reve=now;reverse(reve.begin(),reve.end());//手写的翻转字符串,与上一行效果相同 //for(int k=now.length()-1;k>=0;k--) //rev+=now[k];//cout<<rev<<endl;int maxn=dfs(now,reve,i);if(maxn>max)max=maxn;}}cout<<max<<endl;}return 0;}
暴力:
#include<iostream>#include<stdio.h >#include<string>#include<algorithm>using namespace std;string a[110];int main(){string s1,s2;int t,n,i,j,mini,min,k,max;cin>>t;while(t--){max = 0; min = 200;cin>>n;for(i=0; i<n; i++){cin>>a[i];if(a[i].size() < min){min = a[i].size();mini = i;}}for(i=0; i<a[mini].size(); i++){for(j=1; j<=a[mini].size() - i; j++){s1 = a[mini].substr(i,j); //返回从i开始的 j 个字符组成的字符串s2 = s1;reverse(s2.begin(),s2.end());for(k=0; k<n; k++){//从0开始查找字符串s1,s2 在当前串中的位置,查找成功时返回所在位置,失败返回string::npos的值if(a[k].find(s1,0) == string::npos && a[k].find(s2,0) == string::npos) break;}if(k == n && j > max)max = j;}}cout<<max<<endl;}return 0;}
1 0
- HDU 1238 Substrings (最长公共子串+DFS)
- HDU--1238-Substrings(-最长连续子串)
- 最长公共子串(Longest Common SubStrings)
- HDU1238 Substrings(kmp,最长公共子串)
- HDU 1238 Substrings(求公共正反向连续子串)
- HDU1238 POJ1226 Substrings 最长公共子串(子反串也算)
- poj1226 Substrings (多串最长公共子串+正反)
- poj 1226 hdu 1238 Substrings 求若干字符串正串及反串的最长公共子串 2002亚洲赛天津预选题
- hdu 1238 Substrings (最长相同连续子序列)
- hdu 1238(最长公共子序列变形)
- hdu 1238(最长公共子序列变形)
- HDU 1503 LCS 最长公共子串
- HDU 1159 最长公共子串
- hdu 1159 (最长公共子序列)
- hdu 1159(最长公共子序列)
- hdu 1159(最长公共子序列)
- POJ 1226 Substrings (后缀数组 n个串的最长公共子串)
- 最长公共子串
- Jeecg引领企业开发潮流,开启插件开发时代,微信企业号插件(jeecg-p3-biz-qywx)首次发布
- Chrome 显示不了字
- android输入框EditText输入银行卡,输入手机,输入身份证格式化的实现
- JDBC -----对结果集的系列操作
- Java虚拟机结构分析
- HDU 1238 Substrings (最长公共子串+DFS)
- iOS 8 Xcode6 设置Launch Image 启动图片
- MongoDB学习笔记
- Linux C下TCPsocket编程
- 《twitter:从荒野到IPO》读后感
- MyEclipse创建第一个Servlet程序
- if 与 while
- C语言的指针(进阶篇章之二)
- ssh 连接异常解决纪实(Shared connection to xxx.xxx.xxx.xxx closed.)