标签:ike asc with names log amp lines har ber
Eva would like to make a string of beads with her favorite colors so she went to a small shop to buy some beads. There were many colorful strings of beads. However the owner of the shop would only sell the strings in whole pieces. Hence Eva must check whether a string in the shop contains all the beads she needs. She now comes to you for help: if the answer is “Yes”, please tell her the number of extra beads she has to buy; or if the answer is “No”, please tell her the number of beads missing from the string.
Input Specification:
Each input file contains one test case. Each case gives in two lines the strings of no more than 1000 beads which belong to the shop owner and Eva, respectively.
Output Specification:
For each test case, print your answer in one line. If the answer is “Yes”, then also output the number of
extra beads Eva has to buy; or if the answer is “No”, then also output the number of beads missing from
the string. There must be exactly 1 space between the answer and the number.
Sample Input 1:
ppRYYGrrYBR2258
YrR8RrY
Sample Output 1:
Yes 8
Sample Input 2:
ppRYYGrrYB225
YrR8RrY
Sample Output 2:
No 2
字符串a,b
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char * argv[]){
string a,b;
cin>>a>>b;
int asc[256]={0};
for(int i=0;i<a.length();i++){
asc[a[i]]++;
}
int df=0;
for(int i=0;i<b.length();i++){
if(asc[b[i]]>0)asc[b[i]]--;
else df++;
}
if(df==0)printf("Yes %d",a.length()-b.length());
if(df!=0)printf("No %d",df);
return 0;
}
#include <iostream>
#include <cstring>
using namespace std;
int main(int argc, char * argv[]) {
char s1[1001];
char s2[1001];
cin.getline(s1,1001);
cin.getline(s2,1001);
int len1=strlen(s1),len2=strlen(s2);
int asc1[256]= {0};
int asc2[256]= {0};
for(int i=0; i<len1; i++) asc1[s1[i]]++;
for(int i=0; i<len2; i++) asc2[s2[i]]++;
int df=0;
int ascp[256]= {0};
for(int i=0; i<len2; i++) {
if(ascp[s2[i]]==0&&asc2[s2[i]]>asc1[s2[i]]) {
df+=asc2[s2[i]]-asc1[s2[i]];
ascp[s2[i]]=1;
}
}
if(df==0)printf("Yes %d",len1-len2);
if(df!=0)printf("No %d",df);
return 0;
}
#include <iostream>
#include <cstring>
#include <unordered_map>
using namespace std;
int main(int argc, char * argv[]) {
char s1[1001];
char s2[1001];
cin.getline(s1,1001);
cin.getline(s2,1001);
int len1=strlen(s1),len2=strlen(s2);
unordered_map<char,int> m1,m2;
for(int i=0; i<len1; i++) m1[s1[i]]++;
for(int i=0; i<len2; i++) m2[s2[i]]++;
int df=0;
int ascp[256]= {0};
for(int i=0; i<len2; i++) {
if(ascp[s2[i]]==0&&m2[s2[i]]>m1[s2[i]]) {
df+=m2[s2[i]]-m1[s2[i]];
ascp[s2[i]]=1;
}
}
if(df==0)printf("Yes %d",len1-len2);
if(df!=0)printf("No %d",df);
return 0;
}
PAT Advanced 1092 To Buy or Not to Buy (20) [Hash散列]
标签:ike asc with names log amp lines har ber
原文地址:https://www.cnblogs.com/houzm/p/12239292.html