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
 #include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <windows.h>
void sort(char (*str)[]); //给字符串序列按长度排序
int fun(char (*str)[]); //求发现的最大的子串的长度
//char* in_turn(char* s); //字符串逆序 int main()
{
int t,n;
scanf("%d",&t);
while(t--) //提前判断范围
{
int i=;
char str[][]={};
scanf("%d",&n);
while(i<n)
{
scanf("%s",str[i++]); //gets会接收一个换行符
}
sort(str);
printf("%d\n",fun(str));
}
system("pause>nul");
return ;
} int fun(char (*str)[])
{
int max=,i,j,k;
char s[];
for(k=;str[][k];k++)
{
for(i=;i<=strlen(str[])-k;i++)
{
if(max>i) continue;
int flag=;
strcpy(s,str[]+k);//i-k+1
s[i]='\0';
for(j=;str[j][];j++)
{
if(strstr(str[j],s)==NULL && strstr(str[j],strrev(s))==NULL) //返回一个指向s1中第一次出现s2中字符串的位置,没有返回NULL
{flag=;break;} //strrev()逆序函数
}
if(flag==)
max=i;
}
}
return max;
}
/*
char* in_turn(char* str)
{
char _str[101];
int i;
for(i=0;str[i];i++)
{
_str[strlen(str)-1-i] = str[i];
}
_str[strlen(str)]='\0';
return _str;
}
*/
void sort(char (*str)[]) //一个指向100个char类型的指针
{
int i,j;
char temp[];
for(i=;str[i+][]!='\0';i++)
for(j=i+;str[j][]!='\0';j++)
{
if(strlen(str[i]) > strlen(str[j]))
{
strcpy(temp,str[i]);
strcpy(str[i],str[j]);
strcpy(str[j],temp);
}
}
}

HDU_1238——最大子串搜索的更多相关文章

  1. Java 中字符串的子串搜索

    基友前两天参加了阿里的实习生面试,问了个问题,就是关于字符串的子串搜索的问题.想想实现方式无非就是两层循环,但是 java 中是有现成实现的,于是我就去查查源码,看看 java 语言怎么实现这个的,发 ...

  2. C++自定义String字符串类,支持子串搜索

    C++自定义String字符串类 实现了各种基本操作,包括重载+号实现String的拼接 findSubStr函数,也就是寻找目标串在String中的位置,用到了KMP字符串搜索算法. #includ ...

  3. Substrings 子字符串-----搜索

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

  4. Leetcode 5. Longest Palindromic Substring(最长回文子串, Manacher算法)

    Leetcode 5. Longest Palindromic Substring(最长回文子串, Manacher算法) Given a string s, find the longest pal ...

  5. USACO 6.3 章节 你对搜索和剪枝一无所知QAQ

    emmm........很久很久以前 把6.2过了 所以emmmmmm 直接跳过 ,从6.1到6.3吧 Fence Rails 题目大意 N<=50个数A1,A2... 1023个数,每个数数值 ...

  6. [Golang]Go Packages

    ---------------------------------------------------------------------------------------------------- ...

  7. [转]后缀自动机(SAM)

    原文地址:http://blog.sina.com.cn/s/blog_8fcd775901019mi4.html 感觉自己看这个终于觉得能看懂了!也能感受到后缀自动机究竟是一种怎样进行的数据结构了. ...

  8. 字符串匹配算法1-KMP

    前面介绍过,字符串搜索一般来说有三种方式,前缀搜索,后缀搜索,子串搜索.KMP使用的是前缀搜索. 假设p的偏移是i,也就是窗口的位置是i,匹配到位置j+1时发现了不匹配.现在的问题是向前移动窗口到什么 ...

  9. Remoting接口测试工具

    动手写一个Remoting接口测试工具 基于.NET开发分布式系统,经常用到Remoting技术.在测试驱动开发流行的今天,如果针对分布式系统中的每个Remoting接口的每个方法都要写详细的测试脚本 ...

随机推荐

  1. poj2096 Collecting Bugs(概率dp)

    Collecting Bugs Time Limit: 10000MS   Memory Limit: 64000K Total Submissions: 1792   Accepted: 832 C ...

  2. Collection子接口(List/Set/Queue/SortedSet)

    Collection基本的子接口: List:能够存放反复内容 Set:不能存放反复内容,全部反复的内容靠hashCode()和equals()两个方法区分 Queue:队列接口 SortedSet: ...

  3. Java发送邮件的简单实现

    使用Oracle官方的JavaMail进行实现,JavaMail下载地址:https://java.net/projects/javamail/pages/Home 将下载好的jar包加入到工程路径中 ...

  4. 二分-hdu-4768-Flyer

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4768 题目意思: 有n个A.B.C,每个Ai,Bi,Ci,对于每个P=Ai+k*Ci(P<=B ...

  5. [Redux] Store Methods: getState(), dispatch(), and subscribe()

    console.clear(); const counter = (state = 0, action) => { switch (action.type) { case 'INCREMENT' ...

  6. Windows API一日一练(55)FlushFileBuffers和SetFilePointer函数

    在PC硬件体系结构里,速度最快的存储器是CPU里面的寄存器,接着到二级缓存,再到系统RAM内存,最后才到硬盘.因为这种体系结构,就决定了操作系统对文件的操作方式,或者说是最优化的算法.比方操作系统接收 ...

  7. MySQL创建新用户、增加账户的2种方法及使用实例

    可以用两种方式创建MySQL账户:1.使用GRANT语句2.直接操作MySQL授权表最好的方法是使用GRANT语句,因为这样更精确,错误少.创建超级用户: mysql> GRANT ALL PR ...

  8. Android 百度地图 SDK v3.0.0 (一)

    转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/37729091 最近公司要把百度地图集成的项目中,于是我就研究了一天百度地图的SD ...

  9. Linux查看硬件信息以及驱动设备的命令

    用硬件检测程序kuduz探测新硬件:service kudzu start ( or restart) 查看CPU信息:cat /proc/cpuinfo 查看板卡信息:cat /proc/pci 查 ...

  10. Hyper-V的三种网卡

    External ======= 虚拟机和物理网络.本地主机都能通信 Internal ======= 虚拟机之间互相通信,并且虚拟机能和本机通信 Private ======= 仅允许运行在这台物理 ...