uva-850-Crypt Kicker II

The description of the problem:

                                               give you a sentence which has been decrypted, then give you many lines of cryptograph,and there may be one sentence  matched with the decrypted sentence. if you founded , you must decrypt all the lines and out put them . or else, print No solution.

The algorithm:

                    There are three steps to judge which line is mathched. 1.the lenghth of the sentence.2.the location of the space.3.whether having 26 letters and a letter only match one letter. If the three conditions is meeted, I can get the mapped array of the 26 letters,then I can decrypted all the lines.

The source code:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

char str[1000][200];
char ch[50]={"the quick brown fox jumps over the lazy dog"};
char encode[27];
int num;

int matchall(int t)
{
 int j;
 for(j=0;j<26;j++)
  encode[j]='0';
 for(j=0;j<43;j++)
 {
  if(ch[j]!=' ')
  {
   if(encode[str[t][j]-'a']=='0')
   {
    encode[str[t][j]-'a']=ch[j];
   }
   else if(encode[str[t][j]-'a']!=ch[j])
   {
    return 0;
   }
  }
 }
 for(j=0;j<26;j++)
 {
  if(encode[j]=='0')
   return 0;
 }
 return 1;
}

int judge()
{
 int  i,len;
 for(i=0;i<num;i++)
 {
  len=strlen(str[i]);
  if(len==43)
  {
   if(str[i][3]==' '&&str[i][9]==' '&&str[i][15]==' '&&str[i][19]==' '
    &&str[i][25]==' '&&str[i][30]==' '&&str[i][34]==' '&&str[i][39]==' ')
   {
    if(matchall(i))
     return 1;
   }
  }
 }
 return 0;
}

void print()
{
 int i,j,len;
 for(i=0;i<num;i++)
 {
  len=strlen(str[i]);
  for(j=0;j<len;j++)
  {
   if(str[i][j]!=' ')
    str[i][j]=encode[str[i][j]-'a'];
  }
 }
 for(i=0;i<num;i++)
  printf("%s/n",str[i]);
}

int main()
{
 /*
 freopen(".in","r",stdin);
 freopen(".out","w",stdout);
 */
 int i,t;
 scanf("%d",&t);
 getchar();
 getchar();
 while(t--)
 {
  i=0;
  memset(str,0,sizeof(str));
  while(1)
  {
   gets(str[i]);
   if(strcmp(str[i],"")==0)
    break;
   i++;
  }
  num=i;
  if(judge())
  {
   print();
  }
  else
   printf("No solution./n");
  if(t>=1)
   printf("/n");
 }
 return 0;
}
Sum up:

I got a WA,because I judged the blank line by "gets()!=NULL".But, I do not understand why it is wrong, so I changed the judgement to "strcmp(str,"")==0",then AC.Someone explain it , please.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值