USACO卡题有一段时间了,没想到今天连A三题,看来有进步了????不过,马上要数据库考试了,美国奶牛3.2要暂停了
这题做好很简单,但是输入输出不好处理,输入我是通过1
while(~scanf("%s" , s+strlen(s)));
来处理的,但是在输出上wa了几次,主要是那个回车和空格的处理,还有输出六个的问题
这题可以用map来做,每次取出s中长度a~b的一段给map中,并计数+1,最后再排序,对排序的问题不怎么会用,我是再从考一个pair数组给解决的,C++的STL不是很熟,详看代码1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111/*
ID:shiryuw1
PROG:contact
LANG:C++
*/
using namespace std;
typedef map<string , int> msi;
typedef pair<string ,int> psi;
char s[200005];
string strget(char *t,int k)
{
string str;
for(int i=0;i<k;i++)
str+=t[i];
return str;
}
psi cnt[5555];
int strsec(string s)
{
unsigned int i,ans=0;
for(i=0;i<s.length();i++)
ans=ans*2+s[i]-'0';
return ans;
}
bool cmp(psi a,psi b)
{
if(a.second==b.second)
{
if((a.first.length())==(b.first.length()))
return strsec(a.first)<strsec(b.first);
return (a.first.length())<(b.first.length());
}
return a.second>b.second;
}
int main()
{
freopen("contact.in","r",stdin);
freopen("contact.out","w",stdout);
int a,b,n,i,j;
scanf("%d%d%d",&a,&b,&n);
msi ans;
string tmp;
while(~scanf("%s",s+strlen(s)));
int len=strlen(s);
for(i=0;i<=len-a;i++)
{
tmp=strget(s+i,a);
for(j=a;j<=b&&i+j<=len;j++)
{
if(j!=a)
tmp+=s[i+j-1];
if(!ans.count(tmp))
ans[tmp]=0;
//cout<<tmp<<endl;
ans[tmp]++;
}
}
msi::iterator iter;
for(iter=ans.begin(),i=0;iter!=ans.end();iter++,i++)
{
cnt[i].first=(iter->first);
cnt[i].second=(iter->second);
}
sort(cnt,cnt+(j=i),cmp);
int num=0;
int hang=0;
for(i=0;i<j;i++)
{
if(i&&cnt[i].second==cnt[i-1].second)
{
if(hang==5)
{
cout<<endl;
hang=-1;
}
else
{
cout<<' ';
}
cout<<cnt[i].first;
hang++;
}
else
{
if(num>=n)
break;
if(i)
puts("");
cout<<cnt[i].second<<"\n"<<cnt[i].first;
hang=0;
num++;
}
}
puts("");
return 0;
}