码迷,mamicode.com
首页 > 其他好文 > 详细

1039 到底买不买

时间:2018-08-31 21:06:38      阅读:196      评论:0      收藏:0      [点我收藏+]

标签:color   ==   href   blank   clu   string   target   cin   for   

题目链接:https://pintia.cn/problem-sets/994805260223102976/problems/994805283241443328

题解:

 1 /*
 2 *将字符串中的单个字符变为int值,用数组ans的下表保存。
 3 */
 4 #include <iostream>
 5 #include<string>
 6 using namespace std;
 7 const int MAXN = 200;
 8 
 9 int main() {
10     string str1, str2;
11     cin >> str1 >> str2;
12     int ans[MAXN] = { 0 };
13     for (int i = 0; i < str1.size(); i++)
14         ans[str1[i] - 48]++;
15     int count = 0, cnt = 0;
16     for (int i = 0; i < str2.size(); i++) {
17         int j = 0;
18         for (; j < str1.size(); j++) {
19             if (ans[str1[j] - 48] != 0 && str2[i] == str1[j]) {
20                 ans[str1[j] - 48]--;
21                 count++;
22                 break;//容易忘记
23             }
24         }
25         if (j == str1.size()) cnt++;
26     }
27     if (count == str2.size()) cout << "Yes" << " " << str1.size() - str2.size();
28     else cout << "No" << " " << cnt;
29     return 0;
30 }

 

1039 到底买不买

标签:color   ==   href   blank   clu   string   target   cin   for   

原文地址:https://www.cnblogs.com/Gzu_zb/p/9567413.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!