PAT (Advanced Level) 1071. Speech Patterns (25)

简单题。

#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<map>
#include<stack>
#include<queue>
#include<string>
#include<iostream>
#include<algorithm>
using namespace std;

char s[1048576+10];
string ans[1048576+10];
int tot;

bool g(char c)
{
    if(c>='a'&&c<='z') return 1;
    if(c>='0'&&c<='9') return 1;
    if(c>='A'&&c<='Z') return 1;
    return 0;
}

int main()
{
    gets(s);
    for(int i=0;s[i];i++)
        if(s[i]>='A'&&s[i]<='Z') s[i]=s[i]-'A'+'a';
    string f=""; tot=0;
    for(int i=0;s[i];i++)
    {
        if(g(s[i])) f=f+s[i];
        else
        {
            if(f=="") continue;
            ans[tot++]=f;
            f="";
        }
    }
    if(f!="") ans[tot++]=f;
    sort(ans,ans+tot);
    int now=0,pre=0;
    string out;
    int num=0;

    while(1)
    {
        if(ans[now]==ans[pre]) now++;
        else
        {
            if(now-pre>num)
            {
                num=now-pre;
                out=ans[pre];
            }
            pre=now;
        }
        if(pre==tot) break;
    }

    cout<<out<<" "<<num<<endl;
    return 0;
}
原文地址:https://www.cnblogs.com/zufezzt/p/5634029.html