A. You\'re Given a String...

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
abcd
output
0
input
ababa
output
3
input
zzz
output
2

第一种十分暴力的写法
/* ***********************************************
Author :
Created Time :2015/6/11 20:18:34
File Name :7.cpp
************************************************ */

#include <iostream>
#include <cstring>
#include <cstdlib>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <iomanip>
#include <list>
#include <deque>
#include <stack>
#define ull unsigned long long
#define ll long long
#define mod 90001
#define INF 1<<30
#define maxn 10000+10
#define cle(a) memset(a,0,sizeof(a))
const ull inf = 1LL << 61;
const double eps=1e-5;
using namespace std;

bool cmp(int a,int b){
return a>b;
}
vector<string>v;
int main()
{
#ifndef ONLINE_JUDGE
//freopen("in.txt","r",stdin);
#endif
//freopen("out.txt","iw",stdout);
string s;
while(cin>>s){
v.clear();
string sk=s;
int n=s.size();
for(int i=0;i<n;i++){
string t="";
t+=s[i];
v.push_back(t);
}
for(int i=2;i<n;i++){
for(int j=0;j<n-i+1;j++){
string t="";
for(int k=j;k<j+i;k++){
t+=s[k];
}
v.push_back(t);
}
}
int mark=0;
for(int i=v.size()-1;i>=0;i--){
int pa=s.find(v[i],0);
if(pa==string::npos)continue;
else{
int pb=s.find(v[i],pa+1);
if(pb!=string::npos){
cout<<v[i].size()<<endl;
mark=1;
break;
}
}
}
if(!mark)cout<<0<<endl;
}
return 0;
}
别人的代码
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>
using namespace std;
//这个代码真是写的太好了 真的的的的真的太好了怎么办啊 哈哈 就这样了
typedef long long LL;
typedef map<int,int> MII;
typedef set<int> SI;
typedef vector<int> VI;

#define SZ(x) ((int)(x).size())
#define FR(i,a,b) for(int i=(a);i<(b);++i)
#define MS(a,b) memset((a),b,sizeof(a))
#define EC(tp,it,a) for(tp::iterator it=(a).begin();it!=(a).end();++it)
#define SE(x) cout<<#x<<" = "<<x<<endl
#define PB push_back
#define ALL(x) (x).begin(),(x).end()

template<class T> void inc(T &a, const T &b) { if (a < b) a = b; }
template<class T> void dec(T &a, const T &b) { if (a > b) a = b; }

/*
* Every problem has a simple, fast and wrong solution.
*/


char s[1024];

int main()
{
#ifndef ONLINE_JUDGE
freopen("1.in","r",stdin);
freopen("1.out","w",stdout);
#endif

int ans = 0;
scanf("%s", s);
int n = strlen(s);
s[n] = '*';
for (int i = 0; i + 1 < n; i++)
{
for (int j = i + 1; j < n; j++)
{
int cnt = 0;
while (s[i + cnt] == s[j + cnt])
{
cnt++;
}
if (cnt > ans) ans = cnt;
}
}
printf("%d\n", ans);

return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值