【暴力】日常

链接

Luogu T201838

题目描述

给出一个长度为n的字符串,求这个字符串的最长无相同字符的子串的长度

思路

因为子串最长就26,所以我们直接暴力
时间复杂度就是O(26n)

代码

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>

using namespace std;

int n, ans, ap[105];
char s[3000005];

int main()
{
    
    
	scanf("%d", &n);
	scanf("%s", s + 1);
	for(int i = 1; i <= n; ++i)
	{
    
    
		int sum = 0;
		memset(ap, 0, sizeof(ap));
		for(int j = 1; j <= 26, i + j - 1 <= n; ++j)
		{
    
    
			ap[s[i + j - 1] - 96]++;
			if(ap[s[i + j - 1] - 96] > 1) break;
			sum++;
		}
		ans = max(ans, sum);
	}
	printf("%d", ans);
	return 0;
}

おすすめ

転載: blog.csdn.net/LTH060226/article/details/120649276