Codeforces 23A You're Given a String...

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



转载于:https://www.cnblogs.com/ybwowen/p/8733062.html

EDEM-fluent耦合时出现的问题及摸索出的解决办法及DPM颗粒信息导出 阅读详情

相关推荐

永磁同步电机控制笔记:foc控制原理通俗讲义

引言 到此为止,永磁同步电机控制相关文章已经写了7篇,其中simulink仿真系列文章5篇,控制笔记系列文章2篇,恍惚间感觉少了一篇概论,在此插入一篇,也算是对入坑电机控制一年半来的知识梳理。 文章目录引言一、同步电机是怎么转起来的二、什么样的旋转磁场效率最高三、怎么精准控制旋转磁场的方向四、怎样调节dq轴电流 一、同步电机是怎么转起来的 永磁同步电机结构如下: 外围是定子绕组,中间为永磁体,设...

搞电机的博客 2万+

A. You're Given a String...

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

sdjzqing的专栏 898

直流耦合系统介绍

参考《SMA如何使用直流耦合技术实现更高效的光伏电站-白皮书》

圆桌骑士的博客 731

Day 7 A. You‘re Given a String...

Problem: 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 st

Jackey_tao的博客 270

基于 MATLAB 的 FastICA 算法实现语音信号盲分离

FastICA 算法通过最大化信号的非高斯性,找到独立成分,从而实现信号的分离。这些混合的信号被称为盲源信号,我们希望通过分析混合信号,将它们分离出来以便进行进一步的处理和分析。FastICA 是一种常用的盲源信号分离算法,它基于统计学原理,通过寻找独立成分来实现信号的分离。FastICA(独立成分分析)是一种常用的盲源信号分离算法,在语音和图像处理领域都有广泛的应用。本文将介绍如何使用 MATLAB 实现 FastICA 算法来实现语音信号的盲分离,并提供相应的源代码。FastICA 算法原理。

学习使你进步。 2024

32GB Jetson Orin SOM 不能刷机问题排查

拿到了一个新的Orin SOM,当用orin Devkit CVB或定制的CVB不能用Linux_For_Tegra刷机,flash日志(JetPack5.0DP和5.0.1DP都失败了) 2. 确认mb2配置中将CVB的eeprom读取大小设置是否为0 ? 已经确认是正常的 在定制的orin cvb flash上使用Orin devkit som,启动是成功的。但新得到的Orin som并不工作可以排除理由:使用Orin Devkit CVB和Orin SOM(不是Devkit SOM)仍然不能

jzwjzw19900922的博客 1499

一道字符串题——对substr的使用

You are given a string s consisting of n lowercase Latin letters. You have to type this string using your keyboard. Initially, you have an empty string. Until you type the whole string, you may perfor...

FOWng_lp的博客 852

Autosar进阶专栏索引

Autosar进阶专栏索引

赞的博客 3538

CodeForces - 23A You‘re Given a String...【水题】

题目链接:https://codeforces.com/contest/23/problem/A #include <iostream> #include <algorithm> using namespace std; static const int MAXN=100+10; char s[MAXN]; int ans; int main() { scanf("%s",s); for(int i=0;s[i];i++) for(int j=i+1;

inv0ker的博客 225

边缘设备部署全攻略——ARM架构设备的OpenClaw优化(2026实测)

本文详细介绍了ARM架构设备上OpenClaw的部署与优化方案。针对边缘计算场景,文章系统分析了三类ARM设备:嵌入式设备(如Raspberry Pi 4、Jetson Nano)、工业设备(如BeagleBone、i.MX8M Plus)和服务器设备(如Marvell Armada、AWS Graviton3),并提供了详细的性能与功耗对比数据。在系统配置方面,重点推荐了Ubuntu ARM、Debian ARM和OpenWRT三种操作系统,包括内核版本、必备软件包和针对边缘环境的优化策略(如CPU频率调

博客 410

撸毛笔记-朴朴超市 v1.01

朴朴超市 v1.01脚本地址:自动签到和组队赚朴分, 朴分可以抵扣50%的金额组队瓜分需要4人成团, 每个号每天只能开团和入队各一次每天跑一两次即可使用扫码或自己想办法捉refreshToken扫码获取token: (先微信进入朴朴超市小程序并登录注册过账号再扫码)把获取的refreshToken和备注(非必须)填到文件pupuCookie.txt里(第一次运行会自动创建), 多账号换行或&或@隔开。

xiaoshun007的专栏 1805

You're Given a String... CodeForces - 23A

You're Given a String... CodeForces - 23A 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

codeswarrior的博客 658

C#上位机开发:西门子/三菱PLC通信,新手直接上手

对着西门子S7协议文档看半天,连“DB块地址怎么写”都搞不懂;写三菱PLC代码时,分不清MC协议和Modbus的区别,代码改来改去连不上;好不容易跑通代码,换个PLC型号又报错,不知道哪里出问题;后来发现,新手学PLC通信根本不用先啃协议文档——先跑通“能通信”的代码,再慢慢理解底层逻辑,效率至少提升3倍。西门子PLC:用S7NetPlus库(新手最友好),实现以太网通信,支持S7-200 SMART/1200/1500;

这里只有干货:从 Modbus 通信到 MES 对接,从 YOLO 训练到工控机部署,带你搞定工业软件开发全流程。 877

CodeForces 23A You're Given a String...

Description 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 (

iCpC之旅 1012

温度PID控制PLC程序

温度PID稳定控制程序,适用于欧姆龙PLC,对其他PLC也具有很大参考价值

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

codeforces 23A. You're Given a String...

巨水的一道题,100的数据O(n^3)复杂度30ms过。 蛋疼的是一开始读错题,substring字串认为是subsequence子序列。字串必须连续... 没啥好说的 #include #include #include #include #define max(a,b) ((a)>(b)?(a):(b)) #define INF 1000000005 char a[105]; int m

DBL 672

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

CodeForces - 23A You're Given a String...

题目 由于题目中要求该字符子串出现两次所以直接从头开始进行匹配,第一个for代表该字符子串第一次出现,第二个for代表该字符子串第二次出现。通过max记录最大长度。 #include<iostream> #include<string> #include<cmath> using namespace std; int main( ) { string s; ...

qq_44722533的博客 239

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

CodeForces 23A You're Given a String... (简单题)

题目类型  简单题 题目意思 输入 n (1 排名首先看得分情况 (比赛结果中 赢的一方得3分 输的一方得0分 | 平手情况两队各得一分) 如果得分相同就看 (进球数 - 失球数) 的值, 如果还是相同就看进球数 (都是大的排在前面) 解题方法 成功输入数据后, 统计数据, 最后排序输出即可 (详情看代码) 附 : STL(

yunyouxi0's blog 1019

CodeForces 23A-You're Given a String...

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-cas

wang_128的博客 595

CodeForces 23A You're Given a String...(字符串处理)

Description 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

a novice 1821

洛谷 CF23A You‘re Given a String... 题解(CodeForces)

这个数是子串中最少出现两次的最长子串的长度。因为题目是字串,所以说直接双重。记录某个字符串出现过的次数。时,记录最长的长度。

lhschris的博客 422

You Are Given a Decimal String... CodeForces - 1202B [简单dp][补题]

补一下codeforces前天教育场的题。当时只A了一道题。 大致题意: 定义一个x - y - counter :是一个加法计数器。初始值为0,之后可以任意选择+x或者+y而我们由每次累加结果的最后一位生成一个数列。 例如:4 - 2 - counter 进行+4 +4 +4 +4 +2 +4操作会生成数列 04824。每步要加上x或y是任意的。 给你一个数列(由0~9组...

HK8132185的博客 219

Codeforces 598B Queries on a String 【思维】

B. Queries on a String time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output You are given a string s and should

Joeferyの博客 900

Codeforces 598B Queries on a String 【水题】

B. Queries on a String time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output You are given a string s and should

世界很大 1285

(模拟)关于进制的瞎搞---You Are Given a Decimal String...(Educational Codeforces Round 70 (Rated for Div. 2))...

题目链接:https://codeforc.es/contest/1202/problem/B 题意: 给你一串数,问你插入最少多少数可以使x-y型机器(每次+x或+y的机器,机器每次只取最低位--%10)产生这个子序列。 解: 这题真的是。。。唉我真的,还是怪自己太弱吧,比如08888,8和前一个8相同的话你必须让机器输入东西(不能看着这串数反正都是一样就不输入)。 就是预处...

dgv22747的博客 139

CodeForces - 598B Queries on a String

B. Queries on a String time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output You are given a string s and should

qq_37804582的博客 418

codeforces #306 div 2 A - Two Substrings(随便搞)

A -Two Substrings Time Limit:2000MSMemory Limit:262144KB64bit IO Format:%I64d & %I64u SubmitStatus Description You are given strings. Your task is to determine if t...

weixin_30535565的博客 95
下一篇: 字典树略解
aaa3201
博客等级 码龄11年 4粉丝 0原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值