* Hair candy

1004: 发糖果
Time Limit: 1 Sec Memory Limit: 128 MB 64bit IO Format: %lld
Submitted: 295 Accepted: 96
[Submit][Status][Web Board]
Description

In order to welcome the arrival of 2016, a university held a New Year's Day party. At the end of the party, everyone's welfare came - candy distribution. Before the
candy , assuming that all candies were placed in a straight line. Each person is required to take candy only once, and can only take away candies that are placed consecutively. In order to allow more students to taste
more candies, it is required that each candy can only be taken away by one at most. Sister Byte was very excited when she heard the news and couldn't wait to get candy. In view of Ms. Byte's
wonderful , everyone decided to let her be the first to get candy. So, how many candies can Ms. Byte take at most?

Input

There are multiple sets of test data.
Each set of test data is a string (0<=string length<=10^5), which represents the candies that have been placed. Each string only contains lowercase letters, and different letters represent different types. the
candy

Output

Each set of data output occupies one line, including an integer, representing the maximum number of candies that Ms. Byte took.

Sample Input

aaaaabbbbbbbbbccccccccccdddddd
vchsdgdyejwdsjskassajfshuaw

Sample Output

2
7
Hey~ I found that my comprehension is indeed a problem. I have read the question several times and don't know how to get it.
In fact, you can only take it once, and the candy you take must have no same candy in a row~

#include <stdio.h>
char a[100009];
int main()
{
    while(scanf("%s",a)!=EOF)
    {
        int s[26]={0};
        int cnt=0,max=0;
        for(int i=0;a[i]!='\0';)
        {
            if(s[a[i]-'a']==0)//查重
            {
                s[a[i]-'a']=i+1;//记录当前字母的后一个位置,便于返回
                i++;
                cnt++;
                if(cnt>max)
                    max=cnt;
            }
            else
            {
               i=s[a[i]-'a'];//返回到原来的地方重新计算
                for(int j=0;j<26;j++)
                    s[j]=0;

                 cnt=0;
            }
        }
        printf("%d\n",max);
    }

    return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325364387&siteId=291194637