总时间限制:
1000ms
内存限制:
65536kB
描述
求一个字符串中最长的连续出现的字符,输出该字符及其出现次数。字符串中无空白字符(空格、回车和tab),如果这样的字符不止一个,则输出出现最早的字符。
输入
一行,一个不包含空白字符的字符串,字符串长度小于200。
输出
一行,输出最长的连续出现的字符及其最长的连续出现次数,中间以一个空格分开。
样例输入
aaaaadbbbbbcccccccdddddddddd
样例输出
d 10
源代码:
#include<bits/stdc++.h> using namespace std; char c,newc; int tmp=1,maxn=0; struct node{ char c; int cnt; node(){ cnt=0; } }; vector<node>vec; void update(){ for(int i=0;i<vec.size();i++) if(vec[i].c==c){ if(vec[i].cnt<tmp){ vec[i].cnt=tmp; maxn=max(maxn,tmp); } tmp=1;c=newc; return; } node tmpnode; tmpnode.c=c; tmpnode.cnt=tmp; vec.push_back(tmpnode); maxn=max(maxn,tmp);tmp=1;c=newc; return; } int main(){ scanf("%c",&c); while(scanf("%c",&newc)&&newc!='\n') if(newc==c)tmp++; else update(); update(); for(int i=0;i<vec.size();i++) if(vec[i].cnt==maxn){ cout<<vec[i].c<<" "<<vec[i].cnt; break; } return 0; }