(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
",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
",'a'+i,out[i]);
46         printf("
");
47     }
48     return 0;
49 }
View Code

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

 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
",'a'+i,s[k][i]);
43         printf("
");
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
",c[j],b[n].a[j]);
34             }
35             printf("
");
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
",c[i],b[n].a[i]);
45             }
46             printf("
");
47         }
48     } 
49     return 0;
50 }
原文地址:https://www.cnblogs.com/ACDoge/p/6142880.html