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. Linux---vi编辑器必会操作

    移动光标: (1)基本的上下左右:通过箭头按键控制 (2)跳到一行的末尾:键盘"end" (3)跳到一行的开头:键盘"home" (4)跳到最后一行:shift ...

  2. 本地主机作服务器解决AJAX跨域请求访问数据的方法

    近几天学到ajax,想测试一下ajax样例,由于之前在阿里租用的服务器过期了,于是想着让本地主机既做服务器又做客户端,只是简单地测试,应该还行. 于是,下载了xampp,下载网址http://www. ...

  3. (转载)Delphi TStringList的用法

    Delphi TStringList的用法 TStrings是一个抽象类,在实际开发中,是除了基本类型外,应用得最多的. TStringList 常用方法与属性: var List: TStringL ...

  4. FireMonkey消息机制

    interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Classes, FMX.Forms, FMX.Plat ...

  5. Codeforces 55D Beautiful Number

    Codeforces 55D Beautiful Number a positive integer number is beautiful if and only if it is divisibl ...

  6. AWR报告导出的过程报ORA-06550异常

    刚才在笔记本上(win 7)想要导出一套RAC的AWR报告(linux),执行awrgrpt.sql的脚本的过程中报错了,报错的异常代码是:ORA-06550.经过检查,发现是用户的问题,换成sys用 ...

  7. mongodb 全文检索

    MongoDB 全文检索 全文检索对每一个词建立一个索引,指明该词在文章中出现的次数和位置,当用户查询时,检索程序就根据事先建立的索引进行查找,并将查找的结果反馈给用户的检索方式. 这个过程类似于通过 ...

  8. linux(centos)如何查看文件夹大小

    参考http://zhidao.baidu.com/link?url=OrfDgdHvyA1pSDAy6ql-IgPBWtvcS5AR9bc543zTr1hLIDfCd42nYtNBplAl2pHvM ...

  9. MySQL注入中load_file()函数的应用

    常用的一些Load_File()函数攻击手法:

  10. win7 64下暗黑世界V1.1 服务器端及客户端的安装及运行 成功

    原地址:http://bbs.gameres.com/thread_223717.html 屌丝一枚,没有MAC 更没有XCODE 潜心整了星期六与星期天两天才安装运行成功...只能说安装太复杂了,, ...