Substrings
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 13348   Accepted: 4722

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 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
直接暴力枚举。
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
char s[][];
int n;
int solve()
{
char sub[];
char rsub[];
int len=strlen(s[]);
int cnt=;
for(int l=len;l>;l--)
{
for(int x=;x+l<=len;x++)
{
memset(sub,,sizeof(sub));
memset(rsub,,sizeof(rsub));
strncpy(sub,s[]+x,l);
strcpy(rsub,sub);
reverse(rsub,rsub+strlen(rsub));
cnt=;
for(int i=;i<n;i++)
{
if(strstr(s[i],sub)!=NULL||strstr(s[i],rsub)!=NULL)
{
cnt++;
}
else break;
}
if(cnt==n-) return strlen(sub);
}
}
return ;
}
int main()
{
int cas;
scanf("%d",&cas);
while(cas--)
{
scanf("%d",&n);
memset(s,,sizeof(s));
for(int i=;i<n;i++)
{
scanf("%s",s[i]);
}
printf("%d\n",solve());
}
return ;
}

POJ1226(strstr)的更多相关文章

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

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

  2. [PHP源码阅读]strpos、strstr和stripos、stristr函数

    我在github有对PHP源码更详细的注解.感兴趣的可以围观一下,给个star.PHP5.4源码注解.可以通过commit记录查看已添加的注解. strpos mixed strpos ( strin ...

  3. [LeetCode] Implement strStr() 实现strStr()函数

    Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...

  4. strstr 函数的实现

    strstr函数:返回主串中子字符串的位置后的所有字符. #include <stdio.h> const char *my_strstr(const char *str, const c ...

  5. 28. Implement strStr()

    Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...

  6. Leetcode 详解(Implement strstr)

    Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...

  7. LintCode StrStr

    1. 讨论目标字符串若为空, 则返回-1: 资源字符串若为空, 则返回-1. 2.讨论目标字符串个数为零, 则返回0: 资源字符串个数为零, 则返回-1. 3. 插入旗帜来使第二循环的结束为有条件地返 ...

  8. strstr函数

    原型:char * strstr( char *haystack,  char *needle ) 用法:#include <string.h> 功能:在haystack中寻找needle ...

  9. strstr函数的用法

    C语言函数 编辑 包含文件:string.h 函数名: strstr 函数原型:      extern char *strstr(char *str1, const char *str2); 语法: ...

随机推荐

  1. AngularJS的控制器示例

    代码下载:https://files.cnblogs.com/files/xiandedanteng/angularjsCtrl.rar 代码: <!DOCTYPE HTML PUBLIC &q ...

  2. 指针初始化为NULL的作用

    关于空指针NULL.野指针.通用指针,首先说一下什么是指针,只要明白了指针的含义,你就明白null的含义了. 假设 有语句 int a=10;那么编译器就在内存中开辟1个整型单元存放变量a,我们假设这 ...

  3. Swift----编程语言语法

    1   简单介绍 今天凌晨Apple刚刚公布了Swift编程语言,本文从其公布的书籍<The Swift Programming Language>中摘录和提取而成.希望对各位的iOS&a ...

  4. 构造方法后面带:this()

    可以这么理解,有参数的构造函数需要执行无参构造函数中的代码,为了省去重复代码的编写,所以就继承了,先执行没参数的那个构造函数. 在this上“转到定义”(F12)就到第一个构造函数上去了.  

  5. gulp(基础篇)

    今天在写项目的时候用到了gulp构建工具,虽然一年前就有用过,但是一直只存在于我的“有道云笔记”里,今天又一次用到,固然是巩固一下,这里来记录一下吧:这里我主要想要记录的就是初学者在第一次使用gulp ...

  6. RuntimeWarning: Parent module 'test_project.test_case' not found while handling absolute

    1.Pycharm2016.3.2,导入unittest框架后,运行脚本总是warming,但不影响脚本具体执行 2.通过网上查询,将"C:\Program Files\JetBrains\ ...

  7. java 多线程2(转载)

    http://www.cnblogs.com/DreamSea/archive/2012/01/11/JavaThread.html Ø线程的概述(Introduction) 线程是一个程序的多个执行 ...

  8. 使用 lstat 函数获取文件信息

    前言 在之前的文章中,描述过如何用 fcntl 函数改变文件的状态标记.但,文件还有很多信息,如文件类型,权限设置,设备编号,访问时间等等.如果要获取这些信息,则使用函数 lstat 可以轻松达到这个 ...

  9. [ios]objective-c中Category类别(扩展类)专题总结

    本文转载至 http://yul100887.blog.163.com/blog/static/20033613520126333344127/   objective-c类别的作用?通过类别的方式, ...

  10. c++通用写文件调试代码

    #include <stdio.h>#include <sstream>#include <iostream> std::stringstream strs;str ...