-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cb6c0c0
commit 14f3df4
Showing
2 changed files
with
48 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# A1092 To Buy or Not to Buy | ||
|
||
## 1.题意理解 | ||
给出两个串,第一个是提供的,第二个是需要的,要求判断供给是否能满足需求,若能满足多多少,不能满足差多少。 | ||
|
||
## 2.题目分析 | ||
直接把串中每个字符的个数统计起来即可,可以视作hash的应用,使用map简单实现 | ||
|
||
## 3.参考代码 | ||
```cpp | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
const int MAXL = 1024; | ||
|
||
map<char, int> mp; | ||
|
||
int main() | ||
{ | ||
string buy, want; | ||
int extra = 0, miss = 0; | ||
bool flag = true; | ||
cin >> buy; | ||
cin >> want; | ||
for (char c : buy) | ||
mp[c]++; | ||
for (char c : want) | ||
{ | ||
if (!mp[c]) | ||
{ | ||
flag = false; | ||
miss++; | ||
} | ||
else | ||
mp[c]--; | ||
} | ||
if (flag) | ||
{ | ||
for (auto it : mp) | ||
extra += it.second; | ||
printf("Yes %d\n", extra); | ||
} | ||
else | ||
printf("No %d\n", miss); | ||
system("pause"); | ||
return 0; | ||
} | ||
``` |
Binary file not shown.