http://acm.hdu.edu.cn/showproblem.php?pid=1238

Substrings

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 10   Accepted Submission(s) : 6

Font: Times New Roman | Verdana | Georgia

Font Size: ← →

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

直接枚举可能的字串,然后进行验证。

code:

#include <cstdio>
#include <cstring>
#include <string>
#include <iostream>
using namespace std; string s[102];
int n;
bool check(string sub) {
int i, j;
string tmp;
for(i=0; i<sub.size(); i++)
tmp +=sub[ sub.size()-1-i];
for(i=0; i<n; i++)
if(s[i].find(sub)==s[i].npos && s[i].find(tmp)==s[i].npos)
return false;
return true;
}
void solve(int t,int p) {
string sub;
int i, j, k;
int ans =0;
for(i=0; i<t; i++)
for(j=t-1; j>=i; j--) {
if(j-i+1<ans) continue;
sub = s[p].substr(i,j-i+1);
if(check(sub)) {
if(j-i+1>ans) ans = j-i+1;
}
}
cout<<ans<<endl;
}
int main() {
// freopen("in.txt","r",stdin);
int T,i,t,sub_i, j;
cin>>T;
while(T--) {
cin>>n;
t = 200;
for(i=0; i<n; i++) {
cin>>s[i];
if(s[i].size()<t) {
t =s[i].size();
sub_i = i;
}
}
solve(t, sub_i);
}
return 0;
}


hdu1238 Substrings (暴力)的更多相关文章

  1. HDU-1238 Substrings

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

  2. hdu1238 Substrings 扩展KMP

    You are given a number of case-sensitive strings of alphabetic characters, find the largest string X ...

  3. kuangbin专题十六 KMP&&扩展KMP HDU1238 Substrings

    You are given a number of case-sensitive strings of alphabetic characters, find the largest string X ...

  4. UVa 12718 Dromicpalin Substrings (暴力)

    题意:给定一个序列,问你它有多少上连续的子串,能够重排后是一个回文串. 析:直接暴力,n 比较小不会超时. 代码如下: #pragma comment(linker, "/STACK:102 ...

  5. KMP 、扩展KMP、Manacher算法 总结

    一. KMP 1 找字符串x是否存在于y串中,或者存在了几次 HDU1711 Number Sequence HDU1686 Oulipo HDU2087 剪花布条 2.求多个字符串的最长公共子串 P ...

  6. POJ1226 Substrings ——后缀数组 or 暴力+strstr()函数 最长公共子串

    题目链接:https://vjudge.net/problem/POJ-1226 Substrings Time Limit: 1000MS   Memory Limit: 10000K Total ...

  7. Subsequences in Substrings Kattis - subsequencesinsubstrings (暴力)

    题目链接: Subsequences in Substrings Kattis - subsequencesinsubstrings 题目大意:给你字符串s和t.然后让你在s的所有连续子串中,找出这些 ...

  8. Substrings(hdu1238)字符串匹配

    Substrings Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Sub ...

  9. Java实现 蓝桥杯 算法提高VIP Substrings(暴力)

    试题 算法提高 Substrings 问题描述 You are given a number of case-sensitive strings of alphabetic characters, f ...

随机推荐

  1. redis入门——redis常用命令

    http://blog.csdn.net/wclxyn/article/details/8449082 https://jingyan.baidu.com/article/90bc8fc87ce8e2 ...

  2. No.20 selenium学习之路之文件读写

    1.open 使用open打开文件后一定要记得调用文件对象的close()方法.比如可以用try/finally语句来确保最后能关闭文件. file_object = open('thefile.tx ...

  3. ZOJ 3469 Food Delivery(区间DP好题)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4255 题目大意:在x轴上有n个客人,每个客人每分钟增加的愤怒值不同. ...

  4. C++ 虚函数及重载、重定义、重写

    #include<iostream> usingnamespace std; class BASE { public: BASE()=default; BASE(int publicVal ...

  5. CSS常见简写规则整理

    外边距(margin) margin-top margin-right margin-bottom margin-left 简写顺序为顺时针方向(上.右.下.左),如:margin: 1px 2px ...

  6. hdu 1760 DFS+博弈

    0代表可放 1带表不能放 每次放一个2*2的方块 不能放者败如果先手必胜则输出Yes 必胜态:从当前状态所能到达的状态中存在一个必败态必败态:从当前状态所能达到的状态全部是必胜态 Sample Inp ...

  7. jboss各种测试方式归类

      不跨工程访问(如:HBase) 跨工程访问(如:Business) 不部署到服务器上 部署到服务器上 不部署到服务器上 部署到服务器上 Junit测试 实例化直接调用 true true Fals ...

  8. Web前端开发最佳实践(11):使用更严格的JavaScript编码方式,提高代码质量

    前言 JavaScript语言由于其固有的灵活性,所以导致开发者可以写出很多诡异的代码,甚至一些较为正常的特性,如类型隐式转换.this的指代等等,也会让刚接触此语言的开发者头大不已.尤其是那些熟知其 ...

  9. C语言:输入10个整数,找出其中绝对值最小的数

    1 输入10个整数,找出其中绝对值最小的数(10分) 题目描述 输入10个整数,找出其中绝对值最小的数 输入 十个整数 输出 绝对值最小的数 样例输入 -10 -2 30 40 50 60 70 80 ...

  10. Maven项目使用阿里云的Maven库

    Maven项目下载一些jar包非常慢,有时候一个项目能下一个上午,因此可以考虑使用阿里云的Maven库,因为是国内的,所以下载速度非常酷 单个项目使用阿里云的Maven库: pom文件中 <!- ...