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 ...
随机推荐
- eclipse怎样修改同名包(package)的显示样式、格式
打开我们的项目,可以看到左侧的package看上去特别多,没有层级. 点击Package Explorer右上角的箭头图标. 可以看到“Flat(扁平)”,“Hierarchical(分层)”两个选项 ...
- char str = '1.2.';问题
偶然看到群里老哥问道这个问题 #include <iostream> using namespace std; int main() { char str = '1.2.'; ; } 什么 ...
- redis集群安装2
概要:本文主要介绍如何在Centos7中单机搭建redis集群三主三从,按照本文绝对可以实现该需求,至于先搭建单机版主要为了方便理解redis集群,为下一步开发或生产上redis集群做铺垫.同时本 ...
- 第二章 Django之python安装(1)
Python 安装 Django 由百分百的纯 Python 代码编写而成,因此必须在系统中安装 Python .Django 需要 2.3 或更高版本的 Python.如果使用的是 Linux 或 ...
- Signal Processing and Pattern Recognition in Vision_15_RANSAC:Random Sample Consensus——1981
此部分是 计算机视觉中的信号处理与模式识别 与其说是讲述,不如说是一些经典文章的罗列以及自己的简单点评.与前一个版本不同的是,这次把所有的文章按类别归了类,并且增加了很多文献.分类的时候并没有按照传统 ...
- 【问题】redhat安装dig
转自:https://blog.csdn.net/ricky_hust/article/details/8868131 最终可行的方法 在linux下没有单独的dig和nslookup的包,而是以bi ...
- python-----opencv截取按帧截取视频
最近有需求把一个视频从指定帧截取一部分,demo代码如下: import cv2 video_path = r'F:\temp\temp_0806\1\video\test.dat' videoCap ...
- docker在Windows环境下的安装
Windows环境下安装 docker有两种安装包 一.Docker for Windows(目前只能在 64 位的 Windows10 专业版.企业版.教育版下才能安装) 二.Docker Tool ...
- idou老师教你学Istio 09: 如何用Istio实现K8S Ingress流量管理
前言 在Istio的世界里,如果想把外部的请求流量引入网格,你需要认识并会学会配置Istio Ingress Gateway 什么是Ingress Gateway 由于Kubernetes Ingr ...
- java线程基础巩固---线程间通信快速入门,使用wait和notify进行线程间的数据通信
之前已经对于线程同步相关的知识点进行了详细的学习,这次来学习一下线程间的通信相关的知识,话不多说直接用代码进行演练,以一个简陋的生产者消费者模型来初步了解下线程间通信是怎么一回事. 生产消费者第一版: ...