#include<bits/stdc++.h>
using namespace std;
bool check(char c)
{
if('a'<=c && c<='z')
return true;
if('A'<=c && c<='Z')
return true;
if('0'<=c && c<='9')
return true;
return false;
}
int main()
{
string s;
getline(cin,s);
unordered_map<string,int> mp;
string res;
for(int i=0; i<s.size();i++)
if(check(s[i]))
{
int j=i;
string word;
while(j<s.size() && check(s[j]))
word+=towlower(s[j++]);
i=j;
mp[word]++;
}
string word;
int cnt=-1;
for(auto item:mp)
{
//这里不能写成item.second>=cnt && word<item.first因为见下图
if(item.second>cnt || item.second==cnt && word<item.first)
{
cnt=item.second;
word=item.first;
}
cout << word << " " << cnt << endl;
}
return 0;
}
/*
1.星期几,字符串1和字符串2相同的字母 ABCDEFG
2.小时 是 字符串1和字符串2相同的字符(这个字符包括0~9和A~N)并且记住一定是在上面表示星期几的后面对应的字符
3.分钟 是 字符串3和字符串4所对应的字符(这个字符包括小写和大写字母)
*/
#include <cstdio>
#include <iostream>
using namespace std;
int main()
{
string a, b, c, d;
cin >> a >> b >> c >> d;
int k = 0;
while (true)
{
if (a[k] == b[k] && a[k] >= 'A' && a[k] <= 'G') break;
k ++ ;
}
char weekdays[7][4] = {"MON", "TUE", "WED", "THU", "FRI", "SAT", "SUN"};
printf("%s ", weekdays[a[k] - 'A']);
k ++ ;
while (true)
{
if (a[k] == b[k] && (a[k] >= '0' && a[k] <= '9' || a[k] >= 'A' && a[k] <= 'N')) break;
k ++ ;
}
printf("%02d:", a[k] <= '9' ? a[k] - '0' : a[k] - 'A' + 10);
for (int i = 0;; i ++ )
if (c[i] == d[i] && (c[i] >= 'a' && c[i] <= 'z' || c[i] >= 'A' && c[i] <= 'Z'))
{
printf("%02d\n", i);
break;
}
return 0;
}
这道题有个大坑就是我们计算总分的时候是double类型,将其放到res中时,是将他们转换成int类型来比较的,但是会出现精度问题,所以在放的时候加上1e-8
#include <iostream>
#include <cstring>
#include <unordered_map>
#include <vector>
#include <algorithm>
using namespace std;
struct School
{
string name;
int cnt=0;
double sum=0;
bool operator< (const School &t) const
{
if (sum != t.sum) return sum > t.sum;
if (cnt != t.cnt) return cnt < t.cnt;
return name < t.name;
}
};
int main()
{
int n;
cin >> n;
unordered_map<string, School> hash;
while (n -- )
{
string id, sch;
double grade;
cin >> id >> grade >> sch;
for (auto& c : sch) c = tolower(c);
if (id[0] == 'B') grade /= 1.5;
else if (id[0] == 'T') grade *= 1.5;
hash[sch].sum += grade;
hash[sch].cnt ++ ;
hash[sch].name = sch;
}
vector<School> schools;
for (auto item : hash)
{
item.second.sum = (int)(item.second.sum + 1e-8);
schools.push_back(item.second);
}
sort(schools.begin(), schools.end());
cout << schools.size() << endl;
int rank = 1;
for (int i = 0; i < schools.size(); i ++ )
{
auto s = schools[i];
if (i && s.sum != schools[i - 1].sum) rank = i + 1;
printf("%d %s %d %d\n", rank, s.name.c_str(), (int)s.sum, s.cnt);
}
return 0;
}
Decode Registration Card of PAT
这道题蛋疼的是对于最后一种查询,如果是空则优先输出,否则再依次输出结果,这样就不会超时
#include <iostream>
#include <cstring>
#include <unordered_map>
#include <algorithm>
#include <vector>
using namespace std;
const int N = 10010;
int n, m;
struct node
{
string id;
int grade;
bool operator< (const node &t) const
{
if (grade != t.grade) return grade > t.grade;
return id < t.id;
}
}Node[N];
int main()
{
cin >> n >> m;
for (int i = 0; i < n; i ++ ) cin >> Node[i].id >> Node[i].grade;
for (int k = 1; k <= m; k ++ )
{
string t, c;
cin >> t >> c;
printf("Case %d: %s %s\n", k, t.c_str(), c.c_str());
if (t == "1")
{
vector<node> persons;
for (int i = 0; i < n; i ++ )
if (Node[i].id[0] == c[0])
persons.push_back(Node[i]);
sort(persons.begin(), persons.end());
if (persons.empty()) puts("NA");
else
for (auto person : persons) printf("%s %d\n", person.id.c_str(), person.grade);
}
else if (t == "2")
{
int cnt = 0, sum = 0;
for (int i = 0; i < n; i ++ )
if (Node[i].id.substr(1, 3) == c)
{
cnt ++ ;
sum += Node[i].grade;
}
if (!cnt) puts("NA");
else printf("%d %d\n", cnt, sum);
}
else
{
unordered_map<string, int> hash;
for (int i = 0; i < n; i ++ )
if (Node[i].id.substr(4, 6) == c)
hash[Node[i].id.substr(1, 3)] ++ ;
vector<pair<int, string>> rooms;
for (auto item : hash) rooms.push_back({-item.second, item.first});
sort(rooms.begin(), rooms.end());
if (rooms.empty()) puts("NA");
else
for (auto room : rooms)
printf("%s %d\n", room.second.c_str(), -room.first);
}
}
return 0;
}