(HDU)1708 -- Shopaholic (购物狂)

题目链接:https://vjudge.net/problem/HDU-1708

刚开始写了一个呆萌模拟TLE蠢代码:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cmath>
 5 #include <algorithm>
 6
 7 using namespace std;
 8
 9 char ans[100000];
10 char temp[100000];
11 char s1[100000];
12 char s2[100000];
13
14 int main()
15 {
16     #ifndef ONLINE_JUDGE
17     freopen("test.txt","r",stdin);
18     freopen("test.out","w",stdout);
19     #endif // ONLINE_JUDGE
20
21     int t,n,i,len;
22     scanf("%d",&t);
23     while(t--)
24     {
25         memset(ans,0,sizeof(ans));
26         getchar();
27         scanf("%s %s %d",s1,s2,&n);
28         strcpy(ans,s1);
29         for(i=1;i<n;i++)
30         {
31             strcat(ans,s2);
32             strcpy(temp,ans);
33             memset(ans,0,sizeof(ans));
34             strcpy(ans,s2);
35             memset(s2,0,sizeof(s2));
36             strcpy(s2,temp);
37         }
38         //printf("%s\n",s2);
39         len=strlen(s2);
40         int out[26];
41         memset(out,0,sizeof(out));
42         for(i=0;i<len;i++)
43             out[s2[i]-‘a‘]++;
44         for(i=0;i<26;i++)
45             printf("%c:%d\n",‘a‘+i,out[i]);
46         printf("\n");
47     }
48     return 0;
49 }

后来发现把它转化成斐波那契的递归多好啊,这样用二维数组来存放字符串。

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <cmath>
 4 #include <iostream>
 5 #include <algorithm>
 6 #include <string>
 7 #include <cstdlib>
 8
 9 using namespace std;
10
11 int s[60][26];
12
13 int main()
14 {
15     #ifndef ONLINE_JUDGE
16     freopen("test.txt","r",stdin);
17     freopen("test.out","w",stdout);
18     #endif // ONLINE_JUDGE
19
20     int n,k,len0,len1,i,j;
21     char str0[50],str1[50];
22     scanf("%d",&n);
23     while(n--)
24     {
25         getchar();
26         scanf("%s%s%d",str0,str1,&k);
27         memset(s,0,sizeof(s));
28
29         len0=strlen(str0);
30         for(i=0;i<len0;i++)
31             s[0][str0[i]-‘a‘]++;
32
33         len1=strlen(str1);
34         for(i=0;i<len1;i++)
35             s[1][str1[i]-‘a‘]++;
36
37         for(i=2;i<=k;i++)
38             for(j=0;j<26;j++)
39             s[i][j]=s[i-1][j]+s[i-2][j];
40
41         for(i=0;i<26;i++)
42             printf("%c:%d\n",‘a‘+i,s[k][i]);
43         printf("\n");
44     }
45 }

提交AC,去看别人的思路,发现一个结构体的也不错:

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<string.h>
 4 #include<math.h>
 5 #include<ctype.h>
 6 #include<float.h>
 7 char e[35],f[35];
 8 char c[] = "abcdefghijklmnopqrstuvwxyz";
 9 typedef struct{
10     int a[26];
11 }letter;
12 letter b[51];
13 int main(){
14     int T,n,len1,len2,i,j,t;
15     scanf("%d",&T);
16     getchar();
17     while(T--){
18         scanf("%s%s%d",e,f,&n);
19         getchar();
20         memset(b,0,sizeof(b));
21         len1 = strlen(e);
22         len2 = strlen(f);
23         for(i=0 ;i<len1 ;i++){
24             t = e[i] - ‘a‘;
25             b[0].a[t]++;
26         }
27         for(i=0 ;i<len2 ;i++){
28             t = f[i] - ‘a‘;
29             b[1].a[t]++;
30         }
31         if(n<2){
32             for(j=0 ;j<26 ;j++){
33                 printf("%c:%d\n",c[j],b[n].a[j]);
34             }
35             printf("\n");
36         }
37         else{
38             for(i=2 ;i<=n ;i++){
39                 for(j=0 ;j<26 ;j++){
40                     b[i].a[j] = b[i-1].a[j] + b[i-2].a[j];
41                 }
42             }
43             for(i=0 ;i<26 ;i++){
44                 printf("%c:%d\n",c[i],b[n].a[i]);
45             }
46             printf("\n");
47         }
48     }
49     return 0;
50 }
时间: 2024-08-01 22:42:09

(HDU)1708 -- Shopaholic (购物狂)的相关文章

HDU 1708 Fibonacci String(数学题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1708 Problem Description After little Jim learned Fibonacci Number in the class , he was very interest in it. Now he is thinking about a new thing -- Fibonacci String . He defines : str[n] = str[n-1] + s

HDU 1678 Shopaholic(简单数学题 贪心)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1678 Problem Description Lindsay is a shopaholic. Whenever there is a discount of the kind where you can buy three items and only pay for two, she goes completely mad and feels a need to buy all items in

HDOJ(HDU) 1678 Shopaholic

Problem Description Lindsay is a shopaholic. Whenever there is a discount of the kind where you can buy three items and only pay for two, she goes completely mad and feels a need to buy all items in the store. You have given up on curing her for this

HDU 1708 简单dp问题 Fibonacci String

Fibonacci String Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4568    Accepted Submission(s): 1540 Problem Description After little Jim learned Fibonacci Number in the class , he was very int

Fibonacci String(hdu 1708)

Fibonacci String Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5008    Accepted Submission(s): 1690 Problem Description After little Jim learned Fibonacci Number in the class , he was very int

hdu 1678(Shopaholic )(最大折扣)(水题,cheapest)

Shopaholic Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1693    Accepted Submission(s): 954 Problem Description Lindsay is a shopaholic. Whenever there is a discount of the kind where you ca

HDU 1708 Fibonacci String(斐波那契字串)

Fibonacci String Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 5358    Accepted Submission(s): 1819 Problem Description After little Jim learned Fibonacci Number in the class , he was very in

HDU 1708

思路 :二位数组维护数目. 1 #include<iostream> 2 #include<stdio.h> 3 #include<stdlib.h> 4 #include<memory.h> 5 #include<string.h> 6 #include<algorithm> 7 #include<cmath> 8 #include<stack> 9 const int MAXX= 50000; 10 con

UVa 11369 Shopaholic

题意: 又到了剁手的季节,购物狂们开始行动,超市也开始行动,规定是:每买三件,可以省去1件最便宜的价格. 给出买的商品数,和每个商品的价值,求出购物狂一共赚了多少钱,呵呵. 思路: 把数据从大到小拍序,把3的倍数的商品价值相加,就是答案. 实现:重写C++STL里的sort()函数的比较函数 bool compare(); 代码: 1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 #incl