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

最新文章

  1. nodejs创建http服务器
  2. windows CMD下的命令
  3. 从C#到Objective-C,循序渐进学习苹果开发(4)--代码块(block)和错误异常处理的理解
  4. WordPress实现长篇文章/日志/单页面分页功能效果
  5. iOS 开发者必不可少的 75 个工具,你都会了吗
  6. windows向ubuntu过渡之常用编程软件安装
  7. PCB模擬設計接地的指導原則
  8. 第一次QQ群视频教育有感
  9. openstack私有云布署实践【17 配置文件部份说明】
  10. 论如何把JS踩在脚下 —— JQuery基础及Ajax请求详解
  11. .NET Core中使用AutoMapper
  12. 解决python编码问题
  13. 坚定关于考研或者工作的决定:work
  14. Haproxy小酌
  15. 手写代码 - java.lang.String/StringBuilder 相关
  16. unity加载ab后,场景shader不起效问题(物件表现黑色)
  17. 处理csv和json数据
  18. MUI class=&quot;mui-switch&quot; 开关监听
  19. 51 IP核查询
  20. 【转载】 深度强化学习处理cartpole为什么reward很难超过200?

热门文章

  1. 日历类Calendar
  2. Openstack(Kilo)安装系列之neutron(九)
  3. RelativeSource.TemplatedParent 属性wpf
  4. 【转】 VC++6.0 在Win7 64位下调试,Shift+F5无法退出
  5. Toxophily-数论以及二分三分
  6. Linux系统优化之网络IO调优
  7. iOS 开发之--打测试包的时候报错的解决方法
  8. Pert图简介
  9. 【BZOJ4384】[POI2015]Trzy wieże 树状数组
  10. 用express创建网站出现&quot;$ DEBUG=microbog ./bin/www&quot;的提示