Substrings

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

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
找到最短字符串,比如其长度为5,先取5的子串,再取4的子串...每次遍历其他字符串中是否含有其逆串或顺串
 #include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
/* 原型:char * strncpy(char *dest, char *src, size_t n);   功能:将字符串src中最多n个字符复制到字符数组dest中(它并不像strcpy一样只有遇到NULL才停止复制,而是多了一个条件停止,就是说如果复制到第n个字符还未遇到NULL,也一样停止),返回指向dest的指针。*/
int main()
{
int N,i,j,num;
freopen("in.txt","r",stdin);
cin>>N;
while(N--)
{
char string[][],pos[],inv[],str[];
int min_str=,index,len,flag=;
cin>>num;
for(i=;i<num;i++)
{
cin>>string[i];//相当于把\n换成'\0';
if(strlen(string[i])<min_str)
min_str=strlen(string[i]);
index=i;
}//输入字符串,并找到短字符串
len=min_str;
strcpy(str,string[index]);
while(len>)
{
for(i=;i<=min_str-len;i++)//对于len长的子串可以取多少种;从最长的开始取
{
flag=;//假设该子串符合
strncpy(pos,str+i,len);//并不会把'\0'复制进去,自己加进去
for(j=;j<len;j++)
inv[j]=pos[len-j-];//求出逆向子串;
inv[len]=pos[len]='\0';
for(j=;j<num;j++)
{
if(strstr(string[j],inv)==NULL&&strstr(string[j],pos)==NULL)
{
flag=;
break;
}
}
if(flag)
break;
}
if(flag)
break;
len--;
}
cout<<len<<endl;
}
}

Substrings(hd1238)的更多相关文章

  1. CF451D Count Good Substrings (DP)

    Codeforces Round #258 (Div. 2) Count Good Substrings D. Count Good Substrings time limit per test 2 ...

  2. Substrings(hdu1238)字符串匹配

    Substrings Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Sub ...

  3. Java实现 蓝桥杯 算法提高VIP Substrings(暴力)

    试题 算法提高 Substrings 问题描述 You are given a number of case-sensitive strings of alphabetic characters, f ...

  4. [CF1538E] Funny Substrings (模拟)

    题面 该场 Div. 3 最"难"的一道题:Funny Substrings O I D \tt OID OID 队长喜欢玩字符串,因为 " O n e I n D a ...

  5. HDU-4455 Substrings(DP)

    题目大意:给一个长度为n的整数序列,定义egg(i,j)表示区间[i,j]中不同的数的个数.q次询问,每次询问x,表示求所有长度为x连续区间的 egg 之和. 题目分析:定义dp(len)表示所有长度 ...

  6. 【SPOJ694】Distinct Substrings (SA)

    求不相同子串个数    该问题等价于求所有后缀间不相同前缀的个数..也就是对于每个后缀suffix(sa[i]),将贡献出n-sa[i]+1个,但同时,要减去那些重复的,即为height[i],故答案 ...

  7. C - Distinct Substrings (模板)

    https://vjudge.net/problem/SPOJ-DISUBSTR 有两种方式来求去除重读的子串 #include <bits/stdc++.h> using namespa ...

  8. 【CF1029A】Many Equal Substrings(模拟)

    题意:给定一个长度为n的串s,要求构造一个长度最小的使s出现了k次的串,可以重叠 n<=50,k<=50 思路:计算一下前后缀相同长度 #include<cstdio> #in ...

  9. poj 3415 Common Substrings(后缀数组+单调栈)

    http://poj.org/problem?id=3415 Common Substrings Time Limit: 5000MS   Memory Limit: 65536K Total Sub ...

随机推荐

  1. python 连接操作数据库(二)

    一.我们接着上期的博客继续对ORM框架进行补充,顺便把paramiko模块也给大家讲解一下: 1.ORM框架: 在连接操作数据库的第一个博客中也已经说了,sqlalchemy是一个ORM框架,总结就是 ...

  2. ubuntu安装配置gradle

    在Ubuntu安装Gradle也是很简单.切记请勿使用apt-get安装Gradle.因为Ubuntu源的Gradle实在太旧.安装好基本不能用. 下面是安装步骤: 1.在官网下载最新的Gradle版 ...

  3. apache主目录,配置文件目录结构说明

    apache服务安装成功后,主要的目录结构如下: |-- bin 程序命令目录[apache执行文件的目录如apachectl,htpassed] |-- build |-- cgi-bin 预设给一 ...

  4. Nutch + solr 这个配合不错哦

    因为朋友需要,所以把这个开源组合放在一起试用了下,正在弄,先Mark下. 用的是Nutch1.9,这个比较新,资料比较少,基本上就是用原来的英文WIKI. 首先要注意的是,不要试着在windows下做 ...

  5. Android APP开发需求文档范本

    Android  APP开发需求文档范本 软件需求文档格式的标准写法 1.引言 1.1 编写目的 • 阐明开发本软件的目的: 1.2 项目背景 • 标识待开发软件产品的名称.代码: • 列出本项目的任 ...

  6. perl 爬取csdn

    <pre name="code" class="python">use LWP::UserAgent; use POSIX; use HTML::T ...

  7. 那些年的那些事CISC和RISC发展中的纠缠

    本文来自http://www.cnbeta.com/articles/224544.htm ARM.ARM.ARM,没错ARM仿佛一夜之间就火了,平板.手机等领域随处可见它的影子,甚至已经有人预言未来 ...

  8. HTML5 Canvas Cheat Sheet

    HTML5 Canvas Cheat Sheet HTML5 Canvas Cheat Sheet v1.x

  9. SVN trunk、branch、tag的用法

    Subversion有一个很标准的目录结构,是这样的.比如项目是proj,svn地址为svn://proj/,那么标准的svn布局是svn://proj/|+-trunk+-branches+-tag ...

  10. Unity 4.6 uGUI的点击事件

    因为Unity 4.6刚刚发布,自带的uGUI功能的相关资料还不是很完善,今天刚装的Unity 4.6,想看一下uGUI是否好用,那么开始就今天的学习吧啊! 1,新建一个空的工程.