題目來源ios
小紅想買些珠子作一串本身喜歡的珠串。賣珠子的攤主有不少串五光十色的珠串,可是不願把任何一串拆散了賣。因而小紅要你幫忙判斷一下,某串珠子裏是否包含了所有本身想要的珠子?若是是,那麼告訴她有多少多餘的珠子;若是不是,那麼告訴她缺了多少珠子。數組
爲方便起見,咱們用[0-9]、[a-z]、[A-Z]範圍內的字符來表示顏色。例如在圖1中,第3串是小紅想作的珠串;那麼第1串能夠買,由於包含了所有她想要的珠子,還多了8顆不須要的珠子;第2串不能買,由於沒有黑色珠子,而且少了一顆紅色的珠子。less
圖 1ide
每一個輸入包含 1 個測試用例。每一個測試用例分別在 2 行中前後給出攤主的珠串和小紅想作的珠串,兩串都不超過 1000 個珠子。測試
若是能夠買,則在一行中輸出 Yes
以及有多少多餘的珠子;若是不能夠買,則在一行中輸出 No
以及缺了多少珠子。其間以 1 個空格分隔。flex
ppRYYGrrYBR2258 YrR8RrY
Yes 8
ppRYYGrrYB225 YrR8RrY
No 2
思路1spa
顏色總共有0-9,a-z,A-Z種,用了兩個數組來保存攤主的珠串,以及小紅須要的珠串(數組長度爲123,由於z的ASCII碼最大,爲122)code
輸入的字符串,將字符的出現次數保存到sell和need數組。同時用bool exist數組(初始化false)表示小紅須要的珠串(設爲true)blog
用sell的每一位減去need的每一位,若是不是小紅須要的,就是多餘的;若是是小紅須要的,且sell[I]-need[i]<0,說明缺了ci
思路2
將攤主的珠串和小紅須要的珠串進行比較,相同的字符統一設爲@或者#或者%等符號都ok
接着分別遍歷兩個字符串,攤主中若是有的字符不是咱們設置的符號,就說明有多餘
小紅需求中若是有的字符不是咱們設置的符號,就說明有缺乏
思路3
用一個hashmap保存攤主的字符串,記錄不一樣顏色珠子數量
遍歷小紅的珠串,同時hashmap中對應的顏色數量-1,若是數量小於0,就說明缺乏這一顆珠子
若是都沒有小於0,能夠輸出Yes
思路1
1 #include <iostream> 2 #include <string> 3 using namespace std; 4 5 int sellArr[123] = { 0 }; 6 int needArr[123] = { 0 }; 7 bool exist[123] = { false }; 8 int main() 9 { 10 string sell; 11 string need; 12 int less = 0; 13 int more = 0; 14 int index = 0; 15 cin >> sell >> need; 16 17 if (sell == need){ 18 printf("Yes 0"); 19 return 0; 20 } 21 22 for (int i = 0; i < sell.length(); ++i){ 23 index = (int)(sell[i]); 24 sellArr[index]++; 25 } 26 27 for (int i = 0; i < need.length(); ++i){ 28 index = (int)(need[i]); 29 needArr[index]++; 30 exist[index] = true; 31 } 32 33 for (int i = 48/*0的ASCII*/; i < 123; ++i){ 34 int temp = sellArr[i] - needArr[i]; 35 if (exist[i] != true){ 36 more = more + temp; 37 } 38 else if (exist[i] == true){ 39 if (temp < 0){ 40 less = less - temp; 41 } 42 } 43 } 44 if (less == 0){ 45 printf("Yes %d", more + 1); 46 } 47 else{ 48 printf("No %d", less); 49 } 50 return 0; 51 }
思路2
1 #include <iostream> 2 #include <string> 3 using namespace std; 4 int main() 5 { 6 string sell; 7 string need; 8 int less = 0; 9 int more = 0; 10 11 cin >> sell >> need; 12 for(int i = 0; i < sell.size(); ++i) 13 { 14 for(int j = 0; j < need.size(); ++j) 15 { 16 //爲何不是sell[j]? 17 //有可能 need.size() > sell.size(); 18 //sell[j] 會越界 19 if(need[j] == sell[i]) 20 { 21 need[j] = '@'; 22 sell[i] = '@'; 23 } 24 } 25 } 26 27 for (int i = 0; i < sell.size(); ++i) 28 { 29 if (sell[i] != '@') 30 { 31 more++; 32 } 33 } 34 for (int i = 0; i < need.size(); ++i) 35 { 36 if (need[i] != '@') 37 { 38 less++; 39 } 40 } 41 if (less != 0) 42 { 43 cout << "No " << less; 44 } 45 else 46 { 47 cout << "Yes " << more; 48 } 49 return 0; 50 }
思路3
1 #include <iostream> 2 #include <unordered_map> 3 #include <string> 4 using namespace std; 5 int main() { 6 string sell, need; 7 cin >> sell >> need; 8 bool flag = true; // 是否缺乏 9 unordered_map<char, int> sellMap; 10 int less = 0; // 缺乏的珠子數量 11 12 for (int i = 0; i < sell.size(); ++i) { 13 sellMap[sell[i]]++; 14 } 15 16 for (int i = 0; i < need.size(); ++i) { 17 sellMap[need[i]]--; 18 if (sellMap[need[i]] < 0) { 19 flag = false; 20 less++; 21 } 22 } 23 if (flag) { 24 cout << "Yes " << sell.size() - need.size(); // 多出來的珠子 25 } 26 else { 27 cout << "No " << less; 28 } 29 return 0; 30 }