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 ...
随机推荐
- vue中子组件的created、mounted钩子中获取不到props中的值问题
父子组件通信 这个官网很清楚,也很简单,父组件中使用v-bind绑定传送,子组件使用props接收即可 例如: 父组件中: <template> <div> <head- ...
- py datetime
python datetime模块strptime/strptime format常见格式命令- [python]2011-12-23 版权声明:转载时请以超链接形式标明文章原始出处和作者信息及本 ...
- 题解 P3378 【【模板】堆】
Update 18.2.27----想当年我还用着C..... 看到题解里一堆用C++ STL库中的优先队列,身为C语言选手心里不是滋味 故手打一个优先队列献给坚守在C语言的选手 #include & ...
- Kong命令(二)service
service介绍: service 是声明了一组name.host.port.protocol等配置的函数.可以绑定route.upstream上下游服务.并且对于route.upstream可以绑 ...
- 记录下js几种常见的数组排序和去重的方法
冒泡排序 , , , , , , , ]; function test(){ ; i < arr.length - ; i++){ ; j < arr.length; j++){ var ...
- windows的一些常用指令
持续更新中..... 1.清除系统内 DNS 的缓冲 : nslookup baidu.com 2.修改hosts文件 : 位置 运行 -> C:/windows/system32/ ...
- Java 面向对象(六)接口
一.接口 接口:是Java语言中一种引用类型,是方法的集合,如果说类的内部封装了成员变量.构造方法和成员方法,那么接口的内部主要就是封装了方法,包含抽象方法(JDK 7及以前),默认方法和静态方法(J ...
- 【ASE模型组】Hint::neural 模型与case study
模型 基于搜索的提示系统 我们的系统用Pycee针对语法错误给出提示.然而,对于语法正确.结果错误的代码,我们需要另外的解决方式.因此,我们维护一些 (错误代码, 相应提示) 的数据,该数据可以由我们 ...
- django_redis
目录 下载 说明 补充: 内存中设置值 取值 使用 配置: settings/dev.py缓存配置 - redis存储:依赖 django-redis,要安装>>>pip insta ...
- tsp问题-遍历算法/随机算法
旅行商问题,即TSP问题(Traveling Salesman Problem)又译为旅行推销员问题.货郎担问题,是数学领域中著名问题之一.假设有一个旅行商人要拜访n个城市,他必须选择所要走的路径,路 ...