hdu 1238 Substrings
来源:互联网 发布:手机号码数据库 编辑:程序博客网 时间:2024/06/10 07:53
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6729 Accepted Submission(s): 3004
Problem Description
You are given a number of case-sensitivestrings of alphabetic characters, find the largest string X, such that eitherX, or its inverse can be found as a substring of any of the given strings.
Input
The first line of the input file contains asingle integer t (1 <= t <= 10), the number of test cases, followed bythe input data for each test case. The first line of each test case contains asingle integer n (1 <= n <= 100), the number of given strings, followedby n lines, each representing one string of minimum length 1 and maximum length100. There is no extra white space before and after a string.
Output
There should be one line per test casecontaining 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
题解:
这道题是求在多个字符串中找到最长子序列的长度,这道题用的是列举最短的字符串的每一个子字符串,要列举要用两层for循环,而和每一个字符串比较则需要一个循环,所以一共是3个循环完成列举。
源代码:
#include <iostream>
#include <string.h>
#include <stdio.h>
using namespacestd;
int t,n;
char str[105][105];
char mstr[105];
int maxlen;
bool flag;
void strreverse(char*source,intlen)
{
chartemp;
for(inti = 0;i <len/2;i++)
{
temp= source[i];
source[i]=source[len-i-1];
source[len-i-1]= temp;
}
source[len]='\0';
}
void strmaxlen()
{
charsource[105],resource[105];
for(inti = maxlen;i > 0;i--)
for(intj = 0;j <= maxlen - i;j++)
{
strncpy(source,mstr+j,i);
strncpy(resource,mstr+j,i);
strreverse(resource,i);
source[i]= '\0';
source[i]= '\0';
flag= true;
for(intk = 0;k < n;k++)
if(strstr(str[k],source)==NULL && strstr(str[k],resource)== NULL)
{
flag= false;
break;
}
if(flag)
{
maxlen= i;
return;
}
}
maxlen =0;
}
int main()
{
cin>> t;
for(intx = 0;x < t;x++)
{
cin>> n;
maxlen= 0xffffff;
flag= true;
intl;
for(inti = 0;i < n;i++)
{
scanf("%s",str[i]);
l= strlen(str[i]);
if(l< maxlen)
{
maxlen= l;
strcpy(mstr,str[i]);
}
}
strmaxlen();
printf("%d\n",maxlen);
}
return0;
}
- hdu-1238 Substrings
- HDU 1238 Substrings
- HDU 1238 ( Substrings )
- Hdu 1238 Substrings
- hdu 1238 Substrings
- hdu 1238 Substrings
- HDU 1238 Substrings
- hdu 1238 Substrings
- HDU 1238 Substrings
- hdu 1238 Substrings(KMP)
- hdu 1238 Substrings
- hdu 1238 Substrings
- hdu 1238 Substrings
- HDU 1238 Substrings
- HDU 1238 Substrings
- HDU 1238 Substrings
- HDU 1238 Substrings
- hdu 1238 Substrings
- 如何定义和引用shell中的变量
- [连载]C#程序设计(07)--- C#核心编程-5 --- 数组和字符串
- 10055 - Hashmat the Brave Warrior
- 四舍五入算法、五舍六入、六舍七入、七舍八入。。。。。。。
- win 7c盘下不能粘贴除开文件夹外的其他文件
- hdu 1238 Substrings
- MyEclipse 9.0 安装 Aptana Studio 3插件问题
- iOS申请真机调试证书-图文详解
- matlab 调用C编译器 调试c程序
- android工程中的R.java文件无法生成的原因
- 谈一谈Cocos2d-x中的某些“大小”
- Android开发--WIFI实现
- Scanf系列函数的高级用法
- NYOJ803 A/B Problem【大数】