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 ...
随机推荐
- 帝国cms“建立目录不成功,请检查目录权限”的解决方法
就这个看似简单的问题我折腾了两天,百度看产生这个问题的原因有很多也很宽泛,大部分说的是初始化内置数据,但我出现“建立目录不成功,请检查目录权限”的原因估计只有少部分人会遇到. 内置初始化数据是你上传文 ...
- FPGA上外挂DDR2&DDR3&MIG IP的使用记录
前言 当需要大容量数据存储及处理的时候,FPGA内部自带的存储资源是远远不够的,所以问题来了,怎么使用外带的DDR3? 首要问题在于DDR3是什么?有没有协议?当然只是需要用Xilinx MIG IP ...
- Go Select使用
原文:https://golangbot.com/pointers/ 作者:Nick Coghlan 译者:Noluye 什么是 select? select 语句用于在多个发送/接收信道操作中进行选 ...
- 新人数据库连接不上或数据库配置管理器里面sql服务打不开问题
新人在链接数据库时可能会出现链接不上数据库,这个问题解决方法是大家找到配置管理器里面,把那些停用的功能开启一下应该就可以.但是我们有的电脑还会遇到里面配置里面什么也没有的情况,这个时候我们就得到控制面 ...
- MySQL sql_mode 说明(及处理一起 sql_mode 引发的问题)
1. MySQL莫名变成了 Strict SQL Mode 最近测试组那边反应数据库部分写入失败,app层提示是插入成功,但表里面里面没有产生数据,而两个写入操作的另外一个表有数据.因为 insert ...
- Link monitoring
参考:https://www.ibm.com/support/knowledgecenter/en/linuxonibm/com.ibm.linux.z.l0wlcb00/l0wlcb00_miimo ...
- tp5.1动态获取器 增加一个不存在的字段
$list = $this->agent->where($where) ->withAttr('region',function ($value,$data){ $provice_n ...
- contos 6.6下安装lamp
安装篇 第一步:配置防火墙(默认情况下,端口80和3306是拒绝访问的,在防火墙上进行配置): vi /etc/sysconfig/iptables(在"COMMIT"的上一行加上 ...
- TCP首部介绍
每个T C P段都包含源端和目的端的端口号,用于寻找发端和收端应用进程.这两个值加上I P首部中的源端I P地址和目的端I P地址唯一确定一个T C P连接.有时,一个I P地址和一个端口号也称为一个 ...
- input 设置 display:none后 jquery无法给input赋值
问题出现场景:为什么要用 jquery给input赋值,为什么input带有display:none 在做字典查询的时候,查询参数有字典类型的treeSelect 下拉框,treeSelect渲染后, ...