一道水题但是我错了好惨

罚时罚到最后一名

Problem Description

Time flies, four years passed, colleage is over. When I am about to leave, a xuemei ask me an ACM  problem, but I can't solve it, I am 功力尽失.  Please help me so that I won't lose face in front of xuemei!

 

Give you a string , you should find the longest substring which is of the same character. 

Input

First line there is a T , represents the test cases.

next T lines  will be T strings.

the length of every string is less than 100

all the characters of the strings will be lowercase letters

Output

for each test case output a number

Sample Input

1
a

Sample Output

1
 
 
翻译其实很简单:求给出字符串中最长的相同字符组成的连续子串。。。
可是我一开始居然写成最多字符的个数,跪翻了:
cin>>str;
   len=strlen(str);
   for (i=0;i<=len-1;i++) 
    {
      t=int(str[i]-'a'+1);
      num[t]++;
    }
   best=0; b=0;
   for (i=1;i<=26;i++) {if (num[i]>best) {best=num[i];}}
   cout<<best;

明显错的节奏

这个是我10分钟的代码,似乎是正解:.

cin>>str; i=0; best=0;
   while (i<strlen(str)) 
   {
        t=i;
        ch=str[i];
        while ((t<=strlen(str))&&(str[t]==ch)) t++;
        best=max(best,t-i);
        i=t+1;
   }
   cout<<best<<endl;

然而没过肯定有问题

可是我怎么测都没发现

感谢暖阳(fpcsong)巨大牛。。一个数据搞定:

1

abbc

正解应该是2,可是我答案是1.。。。。。

其实原因在这儿:

i=t+1

应该写:

i=t

哭死啊。。。。。。。。。。。

位处理的时候一定要多看几遍。。。。。。。。

 上正解

代码

#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<cstring>
using namespace std;
char str[200];
char ch;
int len,best,i,t,b,j,all;
int main()
{
   cin>>all;
   for (j=1;j<=all;j++){
   cin>>str; i=0; best=0;
   while (i<strlen(str)) 
   {
        t=i;
        ch=str[i];
        while ((t<=strlen(str))&&(str[t]==ch)) t++;
        best=max(best,t-i);
        i=t;
   }
   cout<<best<<endl;
}
   return 0;
}

结果

RunID用户问题判决时间内存长度提交时间
14306 seekdreamer D Accepted 0 MS 1672 KB C++ 388 B 2014-06-08 23:13:06

再次感谢耐心帮我调出来的暖阳(fpcsong)巨大牛。。。

posted on 2014-06-08 23:32  seekdreamer  阅读(181)  评论(0编辑  收藏  举报