Problem Description
Welcome to 2009 HDU Girl’s Cup, bless you will happy in it.
Every girl are beautiful if you use you heart to feel. Every corner in the world will colourful and energetic by several girls standing. If you boy, a normal bay, I believe that you will try to watch when a beautiful girl passing you and you will nervous if a girl watching you just when you are watching her.
Now give you a surprise that may be never happy in the real time. Millions of PLMM stand in a line and facing you(^_^). They are dress colourful clothings. You should to find out a maximal subline PLMM that their clothing color are all different.
Input
The input contains multiple test cases.
Each case first give a integer n expressing many many girls stand in line.(n<=10000000)
Next line a string including n character, each character standing one girls’s clothing color.
Output
Output one integer the numbers of maximal subline PLMM that their clothing color are all different and the line’s begin and end (based from 0). If their are more answer, print the begin smallest.
Sample Input
3
abc
5
aaaba
8
hdugirls
Sample Output
3 0 2
2 2 3
8 0 7
Author
yifenfei
让你求出这个字符串的最长的那个不重复的子串的长度,开始位置与结束位置(刚开始做就读错了题意,真是英语不好,没法刷题啊。。),思路就是不断维护一个不重复子串的区间,再用一个max更新最长区间的长度就行
我是看了这个博客的思路http://blog.csdn.net/aixiaoling1314/article/details/9288059
完整代码如下:
#include<stdio.h>
#include<string.h>
const int MAX = 1e7+10;
char str[MAX];
int main(void)
{
int n,last[200];//代表这个字符上一次出现的位置
while(scanf("%d",&n) != EOF)
{
memset(last,-1,sizeof(last));
scanf("%s",str);
int max_len = 0,len = 0,x = 0,begin = 0;
/*就是记录每个不重复子串的区间大小,扫描一遍,并不断更新max的值*/
for(int i=0;i<n;i++)
{
if(last[str[i]] == -1 || last[str[i]] < x)//如果这个子串上一次出现的位置,在这个不重复子串的区间外,就把这个字符放入该区间
len++;
else
{
if(len > max_len)
{
max_len = len;
begin = x;
}
x = last[str[i]] + 1;//这里要仔细思考一下。
len = i - last[str[i]];//更新这个新区间的长度
}
last[str[i]] = i;
}
if(len > max_len)//这里也不能少,最后一次也要更新
{
max_len = len;
begin = x;
}
printf("%d %d %d\n",max_len,begin,begin+max_len-1);
}
return 0;
}