HDOJ 1238 Substrings 【最长公共子串】
HDOJ 1238 Substrings 【最长公共子串】
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 11430 Accepted Submission(s): 5490
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
题意
给出一系列字符串 求出这些字符串中的最长公共子串
思路
可以用C++ STL 里面的东西 去找子串
因为题目要求 是可以逆字符串的
所以可以用reverse
然后 查找可以用find
AC代码
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#include <cstdlib>
#include <ctype.h>
#include <numeric>
#include <sstream>
using namespace std;
typedef long long LL;
const double PI = 3.14159265358979323846264338327;
const double E = 2.718281828459;
const double eps = 1e-6;
const int MAXN = 0x3f3f3f3f;
const int MINN = 0xc0c0c0c0;
const int maxn = 1e2 + 5;
const int MOD = 1e9 + 7;
string s[maxn];
int main()
{
int t;
cin >> t;
while (t--)
{
int n;
cin >> n;
int i, j, k;
int sub;
int len = MAXN;
for (i = 0; i < n; i++)
{
cin >> s[i];
if (s[i].size() < len)
{
len = s[i].size();
sub = i;
}
}
int ans = 0;
for (i = s[sub].size(); i > 0; i--)
{
for (j = 0; j < s[sub].size() - i + 1; j++)
{
string s1, s2;
s1 = s[sub].substr(j, i);
s2 = s1;
reverse(s2.begin(), s2.end());
for (k = 0; k < n; k++)
{
if (s[k].find(s1, 0) == -1 && s[k].find(s2, 0) == -1)
break;
}
if (k == n && s1.size() > ans)
ans = s1.size();
}
}
cout << ans << endl;
}
}
HDOJ 1238 Substrings 【最长公共子串】的更多相关文章
- [Data Structure] LCSs——最长公共子序列和最长公共子串
1. 什么是 LCSs? 什么是 LCSs? 好多博友看到这几个字母可能比较困惑,因为这是我自己对两个常见问题的统称,它们分别为最长公共子序列问题(Longest-Common-Subsequence ...
- HDU 1503 带回朔路径的最长公共子串
http://acm.hdu.edu.cn/showproblem.php?pid=1503 这道题又WA了好几次 在裸最长公共子串基础上加了回溯功能,就是给三种状态各做一个 不同的标记.dp[n][ ...
- 最长公共子序列PK最长公共子串
1.先科普下最长公共子序列 & 最长公共子串的区别: 找两个字符串的最长公共子串,这个子串要求在原字符串中是连续的.而最长公共子序列则并不要求连续. (1)递归方法求最长公共子序列的长度 1) ...
- 动态规划(一)——最长公共子序列和最长公共子串
注: 最长公共子序列采用动态规划解决,由于子问题重叠,故采用数组缓存结果,保存最佳取值方向.输出结果时,则自顶向下建立二叉树,自底向上输出,则这过程中没有分叉路,结果唯一. 最长公共子串采用参考串方式 ...
- 字符串hash + 二分答案 - 求最长公共子串 --- poj 2774
Long Long Message Problem's Link:http://poj.org/problem?id=2774 Mean: 求两个字符串的最长公共子串的长度. analyse: 前面在 ...
- 后缀数组(模板题) - 求最长公共子串 - poj 2774 Long Long Message
Language: Default Long Long Message Time Limit: 4000MS Memory Limit: 131072K Total Submissions: 21 ...
- 最长公共子串 NYOJ 36
http://acm.nyist.net/JudgeOnline/problem.php?pid=36 最长公共子序列 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 ...
- POJ 2217 (后缀数组+最长公共子串)
题目链接: http://poj.org/problem?id=2217 题目大意: 求两个串的最长公共子串,注意子串是连续的,而子序列可以不连续. 解题思路: 后缀数组解法是这类问题的模板解法. 对 ...
- poj1159 dp最长公共子串
//Accepted 204 KB 891 ms //dp最长公共子串 //dp[i][j]=max(dp[i-1][j],dp[i][j-1]) //dp[i][j]=max(dp[i][j],dp ...
随机推荐
- EasyUI combobox 加载JSON数据《一》
Action返回 JSON 格式如下: jsonResult = { total=7,rows=[ {TEXT=技术支持, ID=402894ca4419acf1014419b148a10000}, ...
- HashMap与TreeMap的区别?
HashMap与TreeMap的区别? 解答:HashMap通过hashcode对其内容进行快速查找,而TreeMap中所有的元素都保持着某种固定的顺序,如果你需要得到一个有序的结果你就应该使用Tre ...
- 与时间有关的类Date,DateFormat,Calendar
Date类用于表示日期和时间.它没考虑国际化问题,所以又设计了另外两个类. Calendar类: 主要是进行日期字段之间的相互操作. 编程实例:计算出距当前日期时间315天后的日期时间,并使用”xxx ...
- 嵌入式驱动开发之dsp fpga通信接口---spi串行外围接口、emif sram接口
-----------------------------------------author:pkf ------------------------------------------------ ...
- 【BZOJ】3410: [Usaco2009 Dec]Selfish Grazing 自私的食草者(贪心)
http://www.lydsy.com/JudgeOnline/problem.php?id=3410 太神了.... 按末端点排序然后贪心取即可. QAQ #include <cstdio& ...
- (转)java 静态内部类
转自:http://blog.sina.com.cn/s/blog_605f5b4f0100zbps.html 参考:http://docs.oracle.com/javase/tutorial/ja ...
- Dependency Property 依赖属性
依赖属性就是一种可以自己没有值,并能通过使用Binding从数据源获得值(依赖在别人身上)的属性.拥有依赖属性的对象称为“依赖对象”. WPF开发中,必须使用依赖对象作为依赖属性的宿主,使二者结合起来 ...
- 用MathType编辑反三角函数的方法
在使用文档写数学类的文章时候,常常会涉及到一些数学公式,由于数学公式中包含了很多的数学符号,如果使用文档自带的公式编辑器往往会发现很多的符号都不全或者不符合自己的要求.这个时候就需要一款专业的数学公式 ...
- 监视EF生成SQL的方法(log , SqlServerProfile)
大家在学习entityframework的时候,都知道那linq写的叫一个爽,再也不用区分不同RDMS的sql版本差异了,但是呢,高效率带来了差灵活性,我们 无法控制sql的生成策略,所以必须不要让自 ...
- centos使用pypy
pypy最大的特点是使用了jit,可以直接使用机器码而非字节码,大大的提高了效率 ======================== 安装步骤 1.安装pypy yum install pypy* -y ...