类似最集合中最大子序列 hdu 2668

Daydream

时间限制: 1000ms
内存限制: 32768KB
HDU       ID:  2668
64位整型:      Java 类名:

题目描述

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.
类似最集合中最大子序列 hdu 2668_第1张图片
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.

输入

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 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.

样例输入

3
abc
5
aaaba
8
hdugirls

样例输出

3 0 2
2 2 3
8 0 7


n的范围很大,但是经过反复的查看,发现这题和最集合中最大子序列长很象,经过翻看资料,的确

可以用O(n)去做,用一个一维数组就可以了


#include<stdio.h>
#include<string.h>
int a[150];
char s[10000000];
int main()
{
    int n;
    char ch;
    int ben,st,ed,len;
    while(~scanf("%d",&n)){
        ben=st=ed=len=0;
        memset(a,-1,sizeof(a));
        scanf("%s",s);
        for(int i=0;i<n;i++){
            if(a[s[i]] >= ben)     ben=a[s[i]]+1;   //如果发现重复的,回到第一个重复点的下一个位置 
            a[s[i]]=i;
            if(i-ben+1 > len){//如果长度大于原来的长度,起点,终点刷新 
                len=i-ben+1;
                st=ben;
                ed=i;
            }
        }
        printf("%d %d %d\n",len,st,ed);
    }
    return 0;
}



你可能感兴趣的:(HDU)