2009-01-15 16:06:11來源不明

2006 NPSC E. 達文西密碼

其實我不知道為什麼要跑1000個,我原本用strlen(y)結果答案錯!

/***********************************************************/

  1. #include<stdio.h>            
  2. #include<stdlib.h>         
  3. #include<string.h>         
  4. main()         
  5. {         
  6.  int a,b,c,d,temp=0,time=0;         
  7.  char x[1000],y[1000];         
  8.  while(gets(x))              
  9.   {   
  10.    temp=0;time=0;   
  11.   gets(y);       
  12.    a=strlen(x);     
  13.    d=strlen(y);   
  14.    for(b=0;b<a;b++)         
  15.    {         
  16.     if(x[b]!=' ')         
  17.     {   
  18.      time++;       
  19.       for(c=0;c<d;c++)         
  20.       {         
  21.         if(y[c]==x[b])         
  22.         {   
  23.          y[c]=-1;   
  24.          temp++;   
  25.          break;   
  26.         }   
  27.       }         
  28.     }         
  29.    }         
  30.     if(time==temp)         
  31.      printf("Yes\n");         
  32.     else        
  33.      printf("No\n");   
  34.   }            
  35.  return 0;              
  36. }   

/***********************************************/

陣列加快版

#include<stdio.h>        
#include<stdlib.h>     
#include<string.h>     
main()     
{     
 int a,b,c,d,e,n,temp=0,time=0;     
 char x[1000],y[1000];     
 while(gets(x))          
  {  
    gets(y);
    int num[256][2]={0},m=strlen(x);
    for(a=0;a<m;a++)
      {
       num[x[a]][0]++;
       num[y[a]][1]++;
      }
      for(a=0;a<256;a++)
       if(a!=' ')
        if(num[a][0]!=num[a][1]) break;
      if(a==256) printf("Yes\n");
      else printf("No\n"); 
  }        
 return 0;          
}