Acwing第 46 场周赛
作者:互联网
第2题:AcWing 4397. 卡牌
卡住的点:不用具体实现翻牌、记录编号的操作,只需求d[i]=b[i]-a[i],之后将d排序,当d[i]>0时,不选,反之则选。
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 200010;
int n, m;
int a[N], b[N], d[N];
int main()
{
cin >> n >> m;
int sum = 0;
for (int i = 1; i <= n; i ++ ) scanf("%d", &a[i]), sum += a[i];
for (int i = 1; i <= n; i ++ ) scanf("%d", &b[i]), d[i] = b[i] - a[i];
sort(d + 1, d + n + 1);
for(int i = 1; i <= n - m; i ++ ) //最多能翻n-m次
if(d[i] >= 0) break;
else sum += d[i];
printf("%d\n", sum);
return 0;
}
第3题:AcWing 4398. 查询字符串
哈希即可
#include <iostream>
#include <cstring>
#include <algorithm>
#include <unordered_map>
#include <unordered_set>
using namespace std;
int main()
{
unordered_map<string, int> cnt;
unordered_map<string, string> hash;
int n, m;
cin >> n;
while (n -- )
{
unordered_set<string> S;
string str;
cin >> str;
for(int i = 0; i < str.size(); i ++ )
{
string s;
for(int j = i; j < str.size(); j ++ )
{
s += str[j];
S.insert(s);
}
}
for(auto& word: S)
{
cnt[word] ++;
hash[word] = str;
}
}
cin >> m;
while (m -- )
{
string str;
cin >> str;
cout << cnt[str] << " ";
if(!cnt[str]) puts("-");
else cout << hash[str] << endl;
}
return 0;
}
标签:周赛,string,46,cin,int,str,include,unordered,Acwing 来源: https://www.cnblogs.com/esico/p/16127116.html