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
原创粉丝点击