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. Android(java)学习笔记256:JNI之NDK的概念

    1.交叉编译 (1)概念 在一个平台(硬件)和os(软件)环境下,编译出另一种平台和os下可以运行的二进制代码. e.g:     电脑端                               ...

  2. Adding Swap Files

    Adding Swap Files If you do not have free disk space to create a swap partition and you do need to a ...

  3. C# SqlHelper

    操作数据库时,经常会把常用的方法封装到一个类中,这里简单写了一个SQLHelper类,供我平时调用. public static class SqlHelper { private static re ...

  4. wpf的学习日志(一)

    今天开始学习wpf,从xaml的布局开始 stackpanel布局:Orientation决定布局的横向还是纵向,HorizontalAlignment决定布局的对齐 <StackPanel O ...

  5. mockito学习笔记

    mockito http://mockito.github.io/mockito/docs/current/org/mockito/Mockito.html

  6. memcache锁,解决查询过多email查询为空的问题

    /* 设置memcache锁,解决查询过多email查询为空的问题 Begin */ $mmc = new Memcache; $mmc->connect('127.0.0.1', 11211) ...

  7. Android弹性ScrollView

    开袋即食 import android.content.Context; import android.graphics.Rect; import android.util.AttributeSet; ...

  8. 操作sql - 类型初始值设定项引发异常

    这个异常我还是第一次看见,网上有人说,若出现异常,则访问所有的静态成员,均会抛出异常. 在我碰到的问题中,如下代码: ; static private System.Data.DataTable Re ...

  9. SQL2008 存储过程参数相关

      使用inputparame时,使用的是 varchar(20),和数据库中的DEPARTNAME完全匹配,可以查出值: USE [test] GO SET ANSI_NULLS OFF GO SE ...

  10. 第一次启动MySQL时报错

    [root@localhost~]#/usr/local/webserver/mysql/bin/mysql_install_db --basedir=/usr/local/webserver/mys ...