hdu 1238 Substrings (暴搜,枚举)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1238

Substrings

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 8391    Accepted Submission(s): 3862

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

2
3
ABCD
BCDFF
BRCD
2
rose
orchid

Sample Output

2
2

【题意】

在给定字符串中找到最长公共子串, 或者给定字符串子串的反串;

最多100字符串,最长100,直接枚举

求反串可以用 <algorithm>中的reverse函数 ;

【源代码】

#include <iostream>
#include <algorithm>
using namespace std;
string str[110];
int t;
int cmp(const string& a, const string& b){
	return a.length()<b.length();
}
int dfs(string now,string rev,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==rev)
			{
				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);
				string rev=now;
				reverse(rev.begin(),rev.end());
				//for(int k=now.length()-1;k>=0;k--) //原先手写的翻转字符串,用了上一行的reverse 速度从 140ms 下降到109ms
				//	rev+=now[k];
				//cout<<rev<<endl;
				int ma=dfs(now,rev,i);
				if(ma>max)
					max=ma;
			}
		}
		cout<<max<<endl;
	}
	return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-03 20:21:18

hdu 1238 Substrings (暴搜,枚举)的相关文章

HDU 1045 DFS暴搜

Fire Net Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 6229    Accepted Submission(s): 3506 Problem Description Suppose that we have a square city with straight streets. A map of a city is a s

poj 1226 hdu 1238 Substrings 求若干字符串正串及反串的最长公共子串 2002亚洲赛天津预选题

题目:http://poj.org/problem?id=1226 http://acm.hdu.edu.cn/showproblem.php?pid=1238 其实用hash+lcp可能也可以,甚至可能写起来更快,不过我没试,我最近在练习后缀数组,所以来练手 后缀数组的典型用法之一----------------后缀数组+lcp+二分 思路:1.首先将所有的字符串每读取一个,就将其反转,作为一组,假设其下标为i到j,那么cnt[i]到cnt[j]都标记为一个数字(这个数字意思是第几个读入的字符

HDU 5012 bfs暴搜

Dice Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 243    Accepted Submission(s): 135 Problem Description There are 2 special dices on the table. On each face of the dice, a distinct number wa

HDU 1238 Substrings (水)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1238 枚举最短字符串的的每个子串然后暴力....我能怎么办,我也很无奈啊 代码: 1 #define _CRT_SECURE_NO_WARNINGS 2 #include <functional> 3 #include <algorithm> 4 #include <iostream> 5 #include <cstring> 6 #include <ca

HDU 5339 Untitled(暴搜)

Untitled Problem Description There is an integer $a$ and $n$ integers $b_1, \ldots, b_n$. After selecting some numbers from $b_1, \ldots, b_n$ in any order, say $c_1, \ldots, c_r$, we want to make sure that $a \ mod \ c_1 \ mod \ c_2 \ mod \ldots \ m

[HDU 5135] Little Zu Chongzhi&#39;s Triangles (dfs暴搜)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5135 题目大意:给你n条边,选出若干条边,组成若干个三角形,使得面积和最大.输出最大的面积和. 先将边从小到大排序,这样前面的两条边加起来如果不大于第三条边就可以跳出,这是一个存在性条件. dfs(int idx,int now,int cnt,int nowmax)代表我当前处理的是第idx条边,已经加入边集的有cnt条边,当前的边的长度和为now,组成的最大面积和为nowmax. 暴力枚举每个三

HDU 4284 Travel Folyd预处理+dfs暴搜

题意:给你一些N个点,M条边,走每条边要花费金钱,然后给出其中必须访问的点,在这些点可以打工,但是需要先拿到证书,只可以打一次,也可以选择不打工之直接经过它.一个人从1号点出发,给出初始金钱,问你能不能访问所以的点,并且获得所以证书. 题解:目标是那些一定要访问的点,怎么到达的我们不关心,但是我们关系花费最少的路径,而且到达那个点后是一定要打工的,如果只是经过,那么在求花费最少的路径的时候已经考虑过了. 因此先用Folyd求出各个点直接的最短路径,由于N很小,又只要求出一个解,所以直接dfs暴搜

hdu 4400 离散化+二分+BFS(暴搜剪枝还超时的时候可以借鉴一下)

Mines Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1110    Accepted Submission(s): 280 Problem Description Terrorists put some mines in a crowded square recently. The police evacuate all peo

hdu 5305 Friends 【暴搜】

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5305 题意:给一个无向图 , 每条边可以是online边也可以是offline边,问 有多少种方法使得每个节点的online边和offline边一样多 解法:暴搜.记录每个点连接的边数,奇数的直接不可能,偶数的分成两个数组,c1[i]表示i的在线朋友数,c2[i]表示i的离线朋友数,然后一条边一条边搜就行了. 代码: #include <stdio.h> #include <ctime>