Codeforces 23A You're Given a String...
2 seconds
256 megabytes
standard input
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).
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 one number — length of the longest substring that can be met in the string at least twice.
abcd
0
ababa
3
zzz
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...的更多相关文章
- [Educational Codeforces Round 16]E. Generate a String
[Educational Codeforces Round 16]E. Generate a String 试题描述 zscoder wants to generate an input file f ...
- 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 ...
- 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 ...
- 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 ...
- Educational Codeforces Round 9 C. The Smallest String Concatenation 排序
C. The Smallest String Concatenation 题目连接: http://www.codeforces.com/contest/632/problem/C Descripti ...
- Educational Codeforces Round 8 C. Bear and String Distance 贪心
C. Bear and String Distance 题目连接: http://www.codeforces.com/contest/628/problem/C Description Limak ...
- Educational Codeforces Round 9 C. The Smallest String Concatenation —— 贪心 + 字符串
题目链接:http://codeforces.com/problemset/problem/632/C C. The Smallest String Concatenation time limit ...
- Educational Codeforces Round 16 E. Generate a String (DP)
Generate a String 题目链接: http://codeforces.com/contest/710/problem/E Description zscoder wants to gen ...
- Codeforces 1204D2. Kirk and a Binary String (hard version) (dp思路)
题目链接:http://codeforces.com/contest/1204/problem/D2 题目是给定一个01字符串,让你尽可能多地改变1变为0,但是要保证新的字符串,对任意的L,R使得Sl ...
随机推荐
- Ctrl+R快速启动应用程序
1.打开注册表 Ctrl+R搜索框中键入“regedit”,回车打开注册表 2.找到HKEY_LOCAL_MACHINE中的如下路径 HKEY_LOCAL_MACHINE\SOFTWARE\Micro ...
- mysql-8.0.16-winx64的最新安装教程
最近刚学习数据库,首先是了解数据库是什么,数据库.数据表的基本操作,这就面临了一个问题,mysql的安装,我这里下载的是64位的,基于Windows的,以下是在我电脑上的安装过程,希望可以帮助到大家. ...
- 注解@Slf4j使用
我们在写代码的时候需要加入日志打印,如果不想每次都写private final Logger logger = LoggerFactory.getLogger(XXX.class); 那么可以用注解 ...
- K2 BPM_万翼科技携手上海斯歌,全面启动K2平台升级项目_十年专注业务流程管理系统
2019年7月25日,万翼科技和上海斯歌在深圳召开了“2019年K2平台升级项目启动会”.万翼科技核心合伙人何建春.管金华,协同管理支撑组负责人贾磊,K2平台产品负责人黄平显,上海斯歌总裁李明,技术研 ...
- eclipse调试之edit source lookup path解决方案
转自:https://blog.csdn.net/zkn_CS_DN_2013/article/details/48731133
- struct 和class到底有什么区别
我们知道struct是C语言的宠儿,当需要一个复杂类型的时候就需要定义一个struct 比如一个学生结构体,含有三个属性,分别是编号.名字和年龄. typedef struct Student { i ...
- Mybatis在xml文件中处理大于、小于、不等于号的方法
在mapper.xml使用大于.小于等符号会和xml语法冲突,解决冲突有两种方式. 方法一: 使用转义字符: 字符名称 字符符号 转义字符 大于号 > > 小于号 < < 与 ...
- python爬虫伪装技术应用
版权声明:本文为博主原创文章,转载 请注明出处: https://blog.csdn.net/sc2079/article/details/82423865 -写在前面 本篇博客主要是爬虫伪装技术的应 ...
- Cloud Native Weekly |面对云平台宕机,企业如何止损
KubeEdge v0.2发布 KubeEdge在18年11月24日的上海KubeCon上宣布开源的一个开源项目,旨在依托K8S的容器编排和调度能力,实现云边协同.计算下沉.海量设备的平滑接入. Ku ...
- python3 准备
一.前言 1.Python是著名的“龟叔”Guido van Rossum发明的 2.python分为python2和python3两大版本,python2渐渐被淘汰,建议使用python3 3.py ...