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. 为什么要web语义化

    为什么要web语义化 其实wiki的定义里面说得非常清晰了.语义化的好处有三点 有利于搜索 容易兼容不同设备 结构清晰,利于团队的开发.维护

  2. 在CentOS 7下试验Drupal 7

    按顺序安装好Apache.MariaDB和PHP,启动Apache和MariaDB,创建一个UTF-8字符集的数据库. > create database if not exists drupa ...

  3. typedef函数指针那些事

    首先来介绍下函数指针: 函数指针是指向函数的指针变量,即本质是一个指针变量. int (*f) (int x); /* 声明一个函数指针 */ f=func; /* 将func函数的首地址赋给指针f ...

  4. js加载优化

    阻塞特性:       JS 有个很无语的阻塞特性,就是当浏览器在执行JS 代码时,不能同时做其他任何事情,无论其代码是内嵌的还是外部的. 脚本位置:       浏览器在碰到一个引入外部JS 文件的 ...

  5. 新发现一个函数:GradientFill

    位于Msimg32.dll之中 https://msdn.microsoft.com/en-us/library/windows/desktop/dd144957(v=vs.85).aspx

  6. 利用SQL Profiler处理开销较大的查询

    当SQL Server的性能变差时,最可能发生的是以下两件事: 首先,某些查询产生了系统资源上很大的压力.这些查询影响整个系统的性能,因为服务器无法足够快速地服务其他SQL查询. 另外,开销较大的查询 ...

  7. HDU 5775 Bubble Sort(线段树)(2016 Multi-University Training Contest 4 1012)

    原址地址:http://ibupu.link/?id=31 Problem Description P is a permutation of the integers from 1 to N(ind ...

  8. 电子科大POJ "统计单词"

    统计单词 Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) C-sources: ...

  9. 使用Comparable接口的小例子

    代码: public class Student implements Comparable<Student> { private int id; private String name; ...

  10. kiki's game

    欢迎参加——BestCoder周年纪念赛(高质量题目+多重奖励) kiki's game Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: ...