【strstr】#23 A. You're Given a String...
2014-05-30 15:29
399 查看
A. You're Given a String...
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
You're given a string of lower-case Latin letters. Your task is to find the length of its longest substring that can be met in the string at least twice. These occurrences can overlap (see sample test 2).
Input
The first input line contains the string. It's guaranteed, that the string is non-empty, consists of lower-case Latin letters, and its length doesn't exceed 100.
Output
Output one number — length of the longest substring that can be met in the string at least twice.
Sample test(s)
input
output
input
output
input
output
本来吓了一跳……这是DP么……就算是我也不会呀……只能O(n^3)啊……
一看数据……100……
额……直接暴力了…… 这次用的是 strstr(Momstring,Kidstring,Kidlength)函数(返回第一次找到的指针),挺好用的~~
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
You're given a string of lower-case Latin letters. Your task is to find the length of its longest substring that can be met in the string at least twice. These occurrences can overlap (see sample test 2).
Input
The first input line contains the string. It's guaranteed, that the string is non-empty, consists of lower-case Latin letters, and its length doesn't exceed 100.
Output
Output one number — length of the longest substring that can be met in the string at least twice.
Sample test(s)
input
abcd
output
0
input
ababa
output
3
input
zzz
output
2
本来吓了一跳……这是DP么……就算是我也不会呀……只能O(n^3)啊……
一看数据……100……
额……直接暴力了…… 这次用的是 strstr(Momstring,Kidstring,Kidlength)函数(返回第一次找到的指针),挺好用的~~
#include <cstdio> #include <string> #include <cstring> #include <iostream> #include <algorithm> /************************************************* strstr 原型:const char * strstr ( const char * str1, cosnt char *str2); char * strstr ( char * str1, const char * str2); 参数: str1,待查找的字符串指针; str2,要查找的字符串指针。 说明: 在str1中查找匹配str2的子串,并返回指向首次匹配时的第一个元素指针。 如果没有找到,返回NULL指针。 ***************************************************/ using namespace std; int main() { //freopen("in.txt","r",stdin); char line[101]; char find[100]; scanf("%s",line); int len=strlen(line); for(int l=len-1;l>0;l--) { for(int i=0;i<=len-l;i++) { strncpy(find,line+i,l);//从line里的第i位开始,数l个字符的字符串扔给find find[l]='\0';//很重要,不封口简直是作死 if(strstr(strstr(line,find)+1,find))//找到一个,然后在他后头一位开始找第二个 { printf("%d",l); return 0; } } } printf("0"); return 0; }
相关文章推荐
- A. You're Given a String...
- You're Given a String...
- 关于ExtJs4提交数据到jsp页面,返回正确的json显示例子。及error:You're trying to decode an invalid JSON String...解决方法
- error: no type named 'string' in namespace 'std'; did you mean 'std::string'? [3]
- The IP you're using to send email is not authorized
- CodeForces 23A You're Given a String...(字符串处理)
- You're given a string of lower-case Latin letters. Your task is to find the length of its longest su
- CodeForces 23A-You're Given a String...
- [转贴]19 things to think about when you're having a bad day
- Ural 1997-Those are not the droids you're looking for 二分图匹配
- ACM ICPC 2013-2014 H. Those are not the droids you're looking for(二分匹配-Dinic)
- You're Given a String... (kmp)
- 7 Things You're Doing Wrong on LinkedIn
- window.confirm('Are you sure you want to delete it?');
- Unity: Now You're Thinking With Components
- 让国外邮件服务商不再拒收你的邮件,The IP you're using to send mail is not authorized
- There is no READABLE property named 'day' in class 'java.lang.String';You have an error in your SQL
- codeforces 23A. You're Given a String...
- You're Doing It Wrong
- You're Given a String... CodeForces - 23A