zoukankan      html  css  js  c++  java
  • Facebook Hacker Cup 2013 Qualification Round 解题报告

    题目链接:http://www.facebook.com/hackercup/problems.php?pid=475986555798659&round=185564241586420

    题目A :给定一个字符串 ,为每个字符串赋值1-26,使字符串的值最大

    题目考点:数组排序

    解题思路:qsort

    解题代码:

    View Code
     1 // File Name: beautifulstrings.c
     2 // Author: darkdream
     3 // Created Time: 2013年01月26日 星期六 08时09分05秒
     4 
     5 #include<stdio.h>
     6 #include<string.h>
     7 #include<stdlib.h>
     8 #include<time.h>
     9 #include<math.h>
    10 #include<ctype.h>
    11 int cmp(const void *a , const void *b)
    12 {
    13     return *(int *)b - *(int *)a;
    14 }
    15 
    16 int main(){
    17     int n ,j;  
    18     char a[600];
    19 
    20     int b[26];
    21     FILE *p1;
    22     p1 = fopen("output.txt","w");
    23     scanf("%d",&n);
    24     getchar();
    25     for(j = 1; j<= n; j++)
    26     {
    27         gets(a);
    28         memset(b,0,sizeof(b));
    29         int k , l, i ;
    30         for (i = 0 ; i < strlen(a) ; i ++ )
    31             a[i] = toupper(a[i]);
    32 
    33         for (i =  0  ;i <  strlen(a); i++)
    34             if (a[i] >='A' && a[i]<= 'Z')
    35                 b[a[i]-'A'] ++;
    36         qsort(b,26,sizeof(int),cmp);
    37         int sum = 0;
    38         for (i = 0 ; i <26 ;i++  )
    39             sum  += b[i]*(26-i);
    40         fprintf(p1,"Case #%d:%d\n",j,sum);
    41 
    42 
    43     }fclose(p1);
    44     return 0 ;
    45 }

    题目B : 给定一个字符串 有冒号和 括号 看括号是否匹配

    题目考点:逻辑思维

    解题思路: 找到第一个‘(’ 和最后一个‘)’ 外面如果有非笑脸的括号 则不匹配   再看范围内!

    解题代码:

    错误
     1 // File Name: balancedsmileys.c
     2 // Author: darkdream
     3 // Created Time: 2013年01月26日 星期六 09时10分05秒
     4 
     5 #include<stdio.h>
     6 #include<string.h>
     7 #include<stdlib.h>
     8 #include<time.h>
     9 #include<math.h>
    10 
    11 int main(){
    12    int n, j ;
    13     scanf("%d",&n);
    14     getchar();
    15     FILE *p1 = fopen("output.txt","w");
    16     for (j = 1;j <= n; j++)
    17       {
    18         char a[200];
    19         int i , k , l , m , p ,q , sum = 0 ,x,y;
    20         gets(a);
    21         if (strlen(a) == 0)
    22             fprintf(p1,"Case #%d: YES\n",j);
    23         else 
    24         {
    25            for (l = 0 ; l< strlen(a) ;l ++)
    26            {
    27               if (a[l] == '(' )
    28                   break;
    29            }
    30            
    31            for (k = 1 ; k < l ; k ++ )
    32                if (a[k-1] != ':' && a[k] == ')')
    33                    break;
    34            if (k < l )
    35            {  
    36                  fprintf(p1,"Case #%d: NO\n",j);
    37               continue;
    38            }
    39 
    40 
    41         
    42            for (m = strlen(a) ; m >= 0;  m --)
    43            {
    44               if (a[m] == ')' )
    45                   break;
    46            }
    47            
    48            for (k = strlen(a) ; k > m ; k -- )
    49                if (a[k-1] != ':' && a[k] == '(')
    50                    break;
    51 
    52            if (k > m)
    53            {  
    54                  fprintf(p1,"Case #%d: NO\n",j);
    55               continue;
    56            }
    57            p = 0 ;
    58            q = 0 ;
    59            x = 0 ; 
    60            y = 0 ;
    61            for (i  = l ; i <= m ; i ++)
    62            {
    63                if (a[i] == '(')
    64                {   x ++;
    65                   if (i >= 1 && a[i-1] == ':')
    66                       p++;
    67                }
    68                if (a[i] == ')')
    69                {
    70                     y ++;
    71                     if (a[i-1] == ':' )
    72                      q ++;
    73                }   
    74            }
    75            if (x - y > 0 )
    76             {
    77               if  (p >= x - y)
    78                   fprintf(p1,"Case #%d: YES\n",j);
    79               else 
    80                   fprintf(p1,"Case #%d: NO\n",j);
    81             }
    82            else 
    83            {
    84               if (q >= y-x)
    85                   fprintf(p1,"Case #%d: YES\n",j);
    86               else 
    87                   fprintf(p1,"Case #%d: NO\n",j);
    88            }
    89 
    90            
    91         }
    92             
    93        
    94       }
    95     fclose(p1);
    96 return 0 ;
    97 }
    正确
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <algorithm>
     5 #include <vector>
     6 #include <stack>
     7 using namespace std;
     8      
     9 char s[110];
    10      
    11 int main(){
    12     int m,x;
    13     freopen("in.txt","r",stdin);
    14     freopen("out.txt","w",stdout);
    15     scanf("%d",&m);
    16     getchar();
    17     s[0]=' ';
    18     for(int cas=1;cas<=m;cas++){
    19         gets(s+1);
    20         int ans=0;
    21         x=0;
    22         bool jud=1;
    23         for(int i=1;s[i];i++){
    24             if(s[i-1]==':') {
    25                 if(s[i]=='(') x++;
    26                 if(s[i]==')'&&ans>0) ans--,x++;
    27             }
    28             else {
    29                 if(s[i]=='(') ans++;
    30                 if(s[i]==')') {
    31                     if(ans) ans--;
    32                     else if(x) x--;
    33                     else {jud=0;break;}
    34                 }
    35             }
    36         }
    37         if(ans) jud=0;
    38         printf("Case #%d: %s\n",cas,jud?"YES":"NO");
    39     }
    40     return 0;
    41 }

    转自http://hi.baidu.com/isaacpei/item/cd6fa7fb11317a48932af29a

    题目C:给定一个字符串:i位置的字符串是它前K项中没有出现的最小非负整数,并给出了数组前K项的递推关系式!

    题目考点:hash   循环节 同余 

    解题思路 :同余找出前K项 后面K+1项开始循环(应该有更好的算法 大数据测试太慢!)

    解题代码:

    View Code
     1 // File Name: findyourmin.c
     2 // Author: darkdream
     3 // Created Time: 2013年01月26日 星期六 16时28分57秒
     4 
     5 #include<stdio.h>
     6 #include<string.h>
     7 #include<stdlib.h>
     8 #include<time.h>
     9 #include<math.h>
    10 int  h[200010];
    11 long long int d[200010];
    12 int main(){
    13     FILE *p1 = fopen("output.txt","w");
    14     long long j , n , k, a, b, c, r, t , z, i;
    15     scanf("%lld",&t);
    16   for (z =1 ; z <= t ; z++)
    17   {     
    18   memset(h,0,sizeof(h));
    19   memset(d,0,sizeof(d));
    20    scanf("%lld %lld %lld %lld %lld %lld",&n, &k , &a , &b ,&c,&r);
    21    
    22    d[1] = a ;
    23    if (a <= k*2)
    24    h[a] = 1;
    25    for (i =2 ; i <= k ; i++)
    26    {   d[i] =( b%r*d[i-1]%r +c%r)%r;
    27        if (d[i] <= k+1) 
    28           h[d[i]] ++;
    29    }
    30 
    31 
    32    for (i = 1; i <=  k +1   ; i++)
    33    {
    34         for(j = 0; ; j++)
    35             if(h[j] == 0)
    36                 break;
    37       if (d[i] <= k+2)
    38          h[d[i]] -- ;
    39          d[i] = j ;
    40          h[j] ++;
    41        
    42    }
    43    if ((n - k)%(k+1) == 0 )
    44        n = k+1;
    45     else
    46         n = (n-k)%(k+1);
    47    fprintf(p1,"Case #%lld: %lld\n",z,d[n]);
    48 
    49   }
    50 
    51   fclose(p1);        
    52         
    53 return 0 ;
    54 }

     (这题应该先吧b%r 和 a%r 定义了,不然循环开销太大)

  • 相关阅读:
    xcode12.2上运行平板IOS15.1
    发行版本崩溃,调试版本正常
    freetype安装使用
    码流的宽高和裁剪信息
    Xcode 沙盒选项
    【最新揭秘】百度快收权限如何获得,教你一周快速获得快收权限
    destoon提示http 403 forbidden解决方法
    他们口中的百度SEO快排发包参数到底是什么?
    destoon内容页调取相关内容标签
    destoon百度小程序主动推送插件
  • 原文地址:https://www.cnblogs.com/zyue/p/2878530.html
Copyright © 2011-2022 走看看