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.

Examples
input
abcd
output
0
input
ababa
output
3
input
zzz
output
2
其实就是一个字符串的问题,水题一道,但我想跟大家说说不同的做法:
 #include <bits/stdc++.h>
using namespace std;
#define REP(i,n) for((i)=0;(i)<(int)(n);i++)
int main(){
int n,i,j,k;
string s;
cin>>s;
n=s.length();
int ans=;
REP(i,n) REP(j,n) if(i<j){//两个字符串首元素位置
for(k=;;k++) if(i+k>=n||j+k>=n||s[i+k]!=s[j+k]) break;//跑字符串长度,越界或不匹配跳出
ans=max(ans,k);//更新最大值
}
cout<<ans<<endl;
return ;
}
这是最普通的做法了,O(n^3),其实不到。
这个方法好在第三重循环,写的很妙。
 for(k=;;k++) if(i+k>=n||j+k>=n||s[i+k]!=s[j+k]) break;
跑两个字符串的首元素的位置,而不是先跑长度,这个想法很有趣。
之后再跑长度后就可以一位一位跑了,
这样比一个个枚举同样长度的字符串要好很多。
对比一下先跑长度的做法:
#include<bits/stdc++.h>
using namespace std;
int main(){
string s;
cin>>s;
int res=;
for(int l=;l<=s.size();l++){//跑长度
vector<string>v;
for(int i=;i+l-<s.size();i++)
v.push_back(s.substr(i,l));//把当前长度的字符串扔进vector里
sort(v.begin(),v.end());//排序准备匹配
for(int i=;i<v.size()-;i++)//开始匹配
if(v[i]==v[i+]){//匹配成功,保存长度
res=l;
break;
}
}
cout<<res<<endl;
return ;
}
是不是匹配时麻烦很多?
当然,这里通过排序匹配,其实也可以用STL里的Set来做:
#include<bits/stdc++.h>
using namespace std;
int n,m;
string w;
set<string>all;
int main(){
cin>>w;
int n=w.size();
for(int i=n-;i>;i--){//跑长度
all.clear();//清空set
for(int j=;j+i<=n;j++) all.insert(w.substr(j,i));//将子串插入集合
if(all.size()!=n-i+){//集合里元素个数与总个数不一样,说明有重复,成功!
cout<<i<<endl;
return ;
}
}
cout<<<<endl;
return ;
}
是不是也很妙?
总结:本题是水题,但我把水题做出3种解法出来,说明这题质量很好
希望大家在一些优秀的题上不妨多想想,这样信息学水平会提高不少的。
(打比赛时就被这么无聊了,毕竟比赛比谁做的对,不是谁写的更好!)
 


Codeforces 23A You're Given a String...的更多相关文章

  1. [Educational Codeforces Round 16]E. Generate a String

    [Educational Codeforces Round 16]E. Generate a String 试题描述 zscoder wants to generate an input file f ...

  2. Codeforces Round #402 (Div. 2) D. String Game

    D. String Game time limit per test 2 seconds memory limit per test 512 megabytes input standard inpu ...

  3. Educational Codeforces Round 16 E. Generate a String dp

    题目链接: http://codeforces.com/problemset/problem/710/E E. Generate a String time limit per test 2 seco ...

  4. CodeForces Round #527 (Div3) A. Uniform String

    http://codeforces.com/contest/1092/problem/A You are given two integers nn and kk. Your task is to c ...

  5. Educational Codeforces Round 9 C. The Smallest String Concatenation 排序

    C. The Smallest String Concatenation 题目连接: http://www.codeforces.com/contest/632/problem/C Descripti ...

  6. Educational Codeforces Round 8 C. Bear and String Distance 贪心

    C. Bear and String Distance 题目连接: http://www.codeforces.com/contest/628/problem/C Description Limak ...

  7. Educational Codeforces Round 9 C. The Smallest String Concatenation —— 贪心 + 字符串

    题目链接:http://codeforces.com/problemset/problem/632/C C. The Smallest String Concatenation time limit ...

  8. Educational Codeforces Round 16 E. Generate a String (DP)

    Generate a String 题目链接: http://codeforces.com/contest/710/problem/E Description zscoder wants to gen ...

  9. Codeforces 1204D2. Kirk and a Binary String (hard version) (dp思路)

    题目链接:http://codeforces.com/contest/1204/problem/D2 题目是给定一个01字符串,让你尽可能多地改变1变为0,但是要保证新的字符串,对任意的L,R使得Sl ...

随机推荐

  1. 为什么领域模型对于架构师如此重要? https://blog.csdn.net/qq_40741855/article/details/84835212

    为什么领域模型对于架构师如此重要? https://blog.csdn.net/qq_40741855/article/details/84835212 2018年12月05日 14:30:19 绝圣 ...

  2. Idea查看一个类和子类(实现类)的结构图

    选择一个类:右键选择Diagrams-show Diagrams(show Diagrams popup表示悬浮当前窗口) 进入下面类似下面的界面: 如果想查看某个类或接口的子类: 先查看自己本地设置 ...

  3. ElasticSearch做实时OLAP框架~实时搜索、统计和OLAP需求,甚至可以作为NOSQL来使用(转)

    使用ElasticSearch作为大数据平台的实时OLAP框架 – lxw的大数据田地 http://lxw1234.com/archives/2015/12/588.htm 一直想找一个用于大数据平 ...

  4. Mac上搭建Web服务器--Apache

    局域网搭建 Web 服务器测试环境,因为Mac OS X 自带了 Apache 和 PHP 环境,我们只需要简单的启动它就行了. 1.命令:sudo apachectl start Apache服务器 ...

  5. IE浏览器下载后台返回的Excel文件,报错400

    问题描述(见下图): 问题分析: 400是后端没有接收到请求 原因是后端高版本的tomcat不会自动对字符串进行转义 所以,前端把参数值进行转义,即encodeURI(string) 问题处理前代码( ...

  6. docker搭建单机ELK

    yum -y install epel-release yum -y install python-pip // 更新pip pip install --upgrade pip // 安装docker ...

  7. DAY2新手选品原则及供应商选择

    一.新手选品原则(主要是为了起量) 1.净利润率高(容易起量) 2.发货方便,售后方便(发货,打包方便,不易破损,退货率低) 3.具有细分市场优势(衣服->古代衣服,论文排版) 4.市场规模够大 ...

  8. 连接mongodb服务器

    连接mongodb有几种方法 一种是使用mongodb编译时生成的客户端进行连接,就是我们之前介绍过的mongo客户端 另一种是使用各种驱动进行连接 这次使用mongo客户端进行连接,之前我们启动了一 ...

  9. Ubuntu系统---终端下用g++进行c++项目

    Ubuntu系统---终端下用g++进行c++项目 目录 一.编译工具(g++/gcc)和编辑工具(vim/gedit)二.C语言 的编译与运行三.C++语言 的编译与运行四.gcc/g++的详细过程 ...

  10. python实现pow函数(求n次幂,求n次方)

    目录 类型一:求n次幂 类型二:求n开方 类型一:求n次幂 实现 pow(x, n),即计算 x 的 n 次幂函数.其中n为整数.pow函数的实现--leetcode 解法1:暴力法 不是常规意义上的 ...