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

题目
原题

题意

输出一个数。这个数是子串中最少出现两次的最长子串的长度。

思路

首先输入字符串 a a a
用一个 m a p map map记录某个字符串出现过的次数。
当次数 ≥ 2 \ge2 2时,记录最长的长度。
因为题目是字串,所以说直接双重 f o r for for循环就好了。
第一重循环 i i i是枚举起点。
第二重是以 i i i为起点的所有字串。

代码

#include<bits/stdc++.h>
#include<cstring>
#include<queue>
#include<set>
#include<stack>
#include<vector>
#include<map>
#define ll long long
using namespace std;
const int N=1e5+10;
const int M=2023;
const int inf=0x3f3f3f3f;
string a;
map<string,int> mp;
int maxx=2,pos;
int main()
{
	cin>>a;
	for(int i=0;i<a.size();i++)
	{
		string b;
		for(int j=i;j<a.size();j++)
		{
			b+=a[j];
			mp[b]++;
			if(mp[b]>=2 and b.size()>=pos)
			{
				pos=b.size();
			}
		}
	}
	cout<<pos;
	return 0;
}

AC记录

  • 6
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值