一开始想多了以为是AC自动机或者Tire之类的,后来发现直接用map<pair<string,string>,int>模拟即可。
就是想办法处理掉那些多余的空格和逗号句号,其实还是比较啰嗦,慢慢写就好
【代码】
/* ***********************************************
Author :angon
************************************************ */
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <stack>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
#define showtime fprintf(stderr,"time = %.15f\n",clock() / (double)CLOCKS_PER_SEC)
#define lld %I64d
#define REP(i,k,n) for(int i=k;i<n;i++)
#define REPP(i,k,n) for(int i=k;i<=n;i++)
#define scan(d) scanf("%d",&d)
#define scanl(d) scanf("%I64d",&d)
#define scann(n,m) scanf("%d%d",&n,&m)
#define scannl(n,m) scanf("%I64d%I64d",&n,&m)
#define mst(a,k) memset(a,k,sizeof(a))
#define LL long long
#define N 1005
#define mod 1000000007
inline int read(){int s=0;char ch=getchar();for(; ch<'0'||ch>'9'; ch=getchar());for(; ch>='0'&&ch<='9'; ch=getchar())s=s*10+ch-'0';return s;}
map<pair<string,string>,int>mp;
char str[1000];
char first[500],second[500];
string ans1,ans2;
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int j=0,ans=0,flag1=0,flag2=0;
while(gets(str))
{
if(str[0] == '#')
{
map<pair<string,string>,int>::iterator it;
ans = 0;
for(it = mp.begin();it!=mp.end();it++)
{
if(ans < it->second)
{
ans = it->second;
ans1 = (it->first).first ;
ans2 = (it->first).second;
}
}
cout<<ans1<<' '<<ans2<<':';
cout<<ans<<endl;
mp.clear();
j=0;flag1=flag2=0;
continue;
}
int len = strlen(str);
flag1 = 0; flag2 = 0;
for(int i = 0;i<len;i++)
{
while(str[i]==' ' && i<len)
{
i++;
}
if(str[i]==',' || str[i]=='.')
{
flag1 = 0;
mst(second,0);
continue;
}
j=0;
int f=0;
flag2 = 0;
while(str[i]>='a' && str[i]<='z' && i<len)
{
second[j++] = str[i];
i++;
f=1;
flag2=1;
}
if(f) i--;
if(flag1 && flag2)
mp[make_pair(first,second)] ++;
strcpy(first,second);
mst(second,0);
flag1 = flag2;
}
}
return 0;
}