版權聲明:本文爲博主原創文章,未經博主允許不得轉載。 https://blog.csdn.net/Initiallysunny/article/details/80374148
題目描述:給定一個由小寫英文字母構成的字符串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;
}
一年不刷題的我在筆試時竟然沒有做出這道題,原因有兩點:時間緊,平時太依賴編譯器(筆試的時候是直接在網頁裏面敲代碼)。這道題用簡單模擬就可以了,題目說找最大長度,所以應該從最大長度開始找,一次減一,直到找到滿足條件的長度即可。