趋势科技笔试编程题

题目描述:给定一个由小写英文字母构成的字符串S,求不含重复字母的子串的最大长度

^_^题目记得不大清楚了。。。

例如:

输入:abcde

输出:4

#include<stdio.h>
#include<cstdio>
#include<string>
#include<cstring>
#include<string.h>
#include<vector>
#include<algorithm>
#include<queue>
#include<stack>
#include<math.h>
#include<cmath>
#include<iomanip>
#include<stdlib.h>
#include<list>
#include<map>
#include<fstream>
#include<ostream>
#include<iostream>
using namespace std;

string str;
int book[26];
int judge(int l,int r)
{

    int i;
    memset(book,0,sizeof(book));
    for(i = l; i < r; i++)
    {
        if(book[str[i]-'a']==0)
            book[str[i]-'a']++;
        else
            break;
    }
    if(i==r)
        return 1;
    else
        return 0;
}

int main()
{
    cin>>str;
    int i,j,len=str.length();
    for(i = len; i > 1; i--)
    {
        for(j = 0; j + i <= len; j++)
        {
            if(judge(j,j+i)==1)
            {
                cout << i << endl;
                return 0;
            }
        }
    }
    return 0;
}
一年不刷题的我在笔试时竟然没有做出这道题,原因有两点:时间紧,平时太依赖编译器(笔试的时候是直接在网页里面敲代码)。这道题用简单模拟就可以了,题目说找最大长度,所以应该从最大长度开始找,一次减一,直到找到满足条件的长度即可。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值