PAT (Advanced) 1092. To Buy or Not to Buy (20)


原题:1092. To Buy or Not to Buy (20)




using namespace std;
const int maxn = 256;
int h[maxn];

int main()
    string s1, s2;
    while(cin >> s1 >> s2)
        fill(h, h+maxn, 0);
        for(int i = 0; i < s2.size(); i++)
        for(int i = 0; i < s1.size(); i++)
            if(h[s1[i]] > 0) h[s1[i]]--;
        int flag = 0; //在无法满足时,直接记录缺额
        for(int i = 0; i < maxn; i++)
            if(h[i] > 0) flag += h[i];

            printf("Yes %d\n", s1.size() - s2.size());
            printf("No %d\n", flag);
    return 0;



DescriptionnnThe advice to "buy low" is half the formula to success in the bovine stock market.To be considered a great investor you must also follow this problems' advice: n "Buy low; buy lower"nnEach time you buy a stock, you must purchase it at a lower price than the previous time you bought it. The more times you buy at a lower price than before, the better! Your goal is to see how many times you can continue purchasing at ever lower prices. nnYou will be given the daily selling prices of a stock (positive 16-bit integers) over a period of time. You can choose to buy stock on any of the days. Each time you choose to buy, the price must be strictly lower than the previous time you bought stock. Write a program which identifies which days you should buy stock in order to maximize the number of times you buy. nnHere is a list of stock prices: n Day 1 2 3 4 5 6 7 8 9 10 11 12nnPrice 68 69 54 64 68 64 70 67 78 62 98 87nnnThe best investor (by this problem, anyway) can buy at most four times if each purchase is lower then the previous purchase. One four day sequence (there might be others) of acceptable buys is: nDay 2 5 6 10nnPrice 69 68 64 62nInputnn* Line 1: N (1 <= N <= 5000), the number of days for which stock prices are given nn* Lines 2..etc: A series of N space-separated integers, ten per line except the final line which might have fewer integers. nOutputnnTwo integers on a single line: n* The length of the longest sequence of decreasing prices n* The number of sequences that have this length (guaranteed to fit in 31 bits) nnIn counting the number of solutions, two potential solutions are considered the same (and would only count as one solution) if they repeat the same string of decreasing prices, that is, if they "look the same" when the successive prices are compared. Thus, two different sequence of "buy" days could produce the same string of decreasing prices and be counted as only a single solution. nSample Inputnn12n68 69 54 64 68 64 70 67 78 62n98 87nSample Outputnn4 2

Buy the souvenirs Buy the souvenirs


Problem DescriptionrnWhen the winter holiday comes, a lot of people will have a trip. Generally, there are a lot of souvenirs to sell, and sometimes the travelers will buy some ones with pleasure. Not only can they give the souvenirs to their friends and families as gifts, but also can the souvenirs leave them good recollections. All in all, the prices of souvenirs are not very dear, and the souvenirs are also very lovable and interesting. But the money the people have is under the control. They can’t buy a lot, but only a few. So after they admire all the souvenirs, they decide to buy some ones, and they have many combinations to select, but there are no two ones with the same kind in any combination. Now there is a blank written by the names and prices of the souvenirs, as a top coder all around the world, you should calculate how many selections you have, and any selection owns the most kinds of different souvenirs. For instance:rnrnrnrnrnAnd you have only 7 RMB, this time you can select any combination with 3 kinds of souvenirs at most, so the selections of 3 kinds of souvenirs are ABC (6), ABD (7). But if you have 8 RMB, the selections with the most kinds of souvenirs are ABC (6), ABD (7), ACD (8), and if you have 10 RMB, there is only one selection with the most kinds of souvenirs to you: ABCD (10).rn rnrnInputrnFor the first line, there is a T means the number cases, then T cases follow.rnIn each case, in the first line there are two integer n and m, n is the number of the souvenirs and m is the money you have. The second line contains n integers; each integer describes a kind of souvenir. rnAll the numbers and results are in the range of 32-signed integer, and 0<=m<=500, 0


  • 广告
  • 抄袭
  • 版权
  • 政治
  • 色情
  • 无意义
  • 其他