Substrings

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6238    Accepted Submission(s): 2767

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
 
Author
Asia 2002, Tehran (Iran), Preliminary
 
Recommend
Eddy
 //寻找最长字串并输出其长度
// 思路:
// 1、先寻找其中最短字符串str,求子字符串就要先找最短的字符串
// 2、根据str搜索满足条件的子字符串
// 3、对str的各子字符串从长到短一次判断是否满足条件,直到找到一个符合条件的子字符串为止
//strlen:计算字符串长度
//strstr:在字符串中寻找子字符串,它的用法用的少,但是碰到了,就要熟练。
//strncpy:复制字符串中的字串,它的用法要掌握。
//strcpy:复制字符串
//怎样找最短字符串,也是值得学习的。。
//搜索满足条件的最长子串.
#include<stdio.h>
#include<string.h>
char a[][];
int n;
void cmp(char *str)//字符串反序
{
int l,i,g=;
char b[];
l=strlen(str);
for(i=;i<l;i++)
b[g++]=str[l-i-];
b[g]='\0';
strcpy(str,b);
}
int search(char *str)//搜索满足条件的最长子串并返回其长度
{
int l1,l2,i,j,f;
char s[],r[];
l1=strlen(str);l2=strlen(str);
while(l1>)//搜索不同长度的子串,从最长的子串开始搜索
{
for(i=;i<=l2-l1;i++)
{
strncpy(s,str+i,l1);//strncpy的函数用法必须是str+i不能写成str【i】;
strncpy(r,str+i,l1);
s[l1]=r[l1]='\0';
cmp(r);
f=;
for(j=;j<n;j++)
if(strstr(a[j],s)==NULL && strstr(a[j],r)==NULL)
{
f=;
break;
}
if(f==)
return l1;
}
l1--;
}
return ;
} int main()
{
int i,minstrlen,substrlen,t;//minstrlen记录最短字符串的长度,substrlen返回最长子串长度
char minstr[];
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
minstrlen=;//先假设字符串最小长度值为100
for(i=;i<n;i++)
{
scanf("%s",a[i]);
if(strlen(a[i])<minstrlen)//寻找n个字符串中最短的字符串
{
strcpy(minstr,a[i]);
minstrlen=strlen(minstr);
}
}
substrlen=search(minstr);
printf("%d\n",substrlen);
}
return ;
}
 

HDU-1238 Substrings的更多相关文章

  1. hdu 1238 Substrings(kmp+暴力枚举)

    Problem Description You are given a number of case-sensitive strings of alphabetic characters, find ...

  2. (KMP 字符串处理)Substrings -- hdu -- 1238

    http://acm.hdu.edu.cn/showproblem.php?pid=1238 Substrings Time Limit:1000MS     Memory Limit:32768KB ...

  3. hdu 4455 Substrings(计数)

    题目链接:hdu 4455 Substrings 题目大意:给出n,然后是n个数a[1] ~ a[n], 然后是q次询问,每次询问给出w, 将数列a[i]分成若干个连续且元素数量为w的集合,计算每个集 ...

  4. HDOJ 1238 Substrings 【最长公共子串】

    HDOJ 1238 Substrings [最长公共子串] Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Ja ...

  5. Substrings - HDU 1238(最大共同子串)

    题目大意:给你N个串,求出来他们的最大公共子串的长度(子串反过来也算他们的子串).   分析:很久以前就做过这道题,当时是用的strstr做的,不过相同的都是枚举了子串......还是很暴力,希望下次 ...

  6. HDU 4455.Substrings

    Substrings Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  7. hdu 4455 Substrings(找规律&DP)

    Substrings Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tota ...

  8. hdu 4455 Substrings (DP 预处理思路)

    Substrings Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tota ...

  9. HDU 4455 Substrings[多重dp]

    Substrings Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  10. HDU - 4455 Substrings(非原创)

    XXX has an array of length n. XXX wants to know that, for a given w, what is the sum of the distinct ...

随机推荐

  1. C# IO操作磁盘上的txt

    using System.IO; //写入并导出到磁盘 StreamWriter sw = new StreamWriter(@"H:\text.txt"); sw.WriteLi ...

  2. DOM五大对象

    1.Window 对象:Window 对象表示浏览器中打开的窗口. 如果文档包含框架(frame 或 iframe 标签),浏览器会为 HTML 文档创建一个 window 对象,并为每个框架创建一个 ...

  3. cin、cin.get()、cin.getline()、getline()、gets()等函数的用法

    学C++的时候,这几个输入函数弄的有点迷糊:这里做个小结,为了自己复习,也希望对后来者能有所帮助,如果有差错的地方还请各位多多指教(本文所有程序均通过VC 6.0运行)转载请保留作者信息:1.cin1 ...

  4. Codeforces 616E - Sum of Remainders

    616E Sum of Remainders Calculate the value of the sum: n mod 1 + n mod 2 + n mod 3 + - + n mod m. As ...

  5. Linux和Windows下查看环境变量方法对比

    摘自:Linux和Windows下查看环境变量方法对比 一.查看所有环境变量的名称和值 Linux下:export Windows下:set 二.根据名称查该环境变量的值 Linux下:echo $环 ...

  6. linux下定时发送邮件

    at命令可以在某个时间运行某个程序,而mail可以以命令行的方式把存于一个文本中的邮件正文发送抄送出去. 具体用法:  1. 把email正文准备好,比如写在email.txt里  2. 然后写一个脚 ...

  7. 《有限元分析基础教程》(曾攀)笔记一-二维杆单元有限元程序(基于Python)

    曾攀老师的<有限元分析基础教程>第三章有二维杆单元的推导,并结合一个例题进行了解析解和基于Matlab的程序求解.但是我感觉书中的MATLAB代码有点罗嗦,而且一些实现方法也比较麻烦,比如 ...

  8. [转载]WCF 几种常见错误

    WCF标准的配置文件为: <system.serviceModel>         <services>             <service name=" ...

  9. DOM in Angular2

    <elementRef> import {ElementRef} from "@angular/core"; constructor(private element:  ...

  10. ASP.NET MVC中HttpContext, HttpContextBase, HttpContextWrapper联系

    ttpContext HttpContext是最原始的ASP.NET Context. MVC的目的之一是能够单元测试.HttpContext没有base class,并且不是virtual,所以不能 ...