zoukankan      html  css  js  c++  java
  • Spell checker(串)

    http://poj.org/problem?id=1035

    题意:给定一个单词判断其是否在字典中,若存在输出"%s is correct",否则判断该单词删掉一个字母,或增加一个字母,或替换一个字母后是否存在于字典中。

     1 #include<stdio.h>
     2 #include<string.h>
     3 int deal(char *s1,char *s2)
     4 {
     5     int i = 0;
     6     int len1 = strlen(s1);
     7     int len2 = strlen(s2);
     8     if(len1==len2)//替换
     9     {
    10         while(i < len1 && s1[i]==s2[i])
    11             i++;
    12         while(++i < len1)
    13         {
    14             if (s1[i]!=s2[i])
    15                 return 0;
    16         }
    17     }
    18     else if (len2-1==len1)//添加
    19     {
    20         while(i < len1 && s1[i]==s2[i])
    21             i++;
    22         while(++i < len2)
    23         {
    24             if (s1[i-1]!=s2[i])
    25                 return 0;
    26         }
    27     }
    28     else if (len2+1==len1)//删除
    29     {
    30         while(i < len2 && s1[i]==s2[i])
    31             i++;
    32         while(++i < len1)
    33         {
    34             if (s1[i]!=s2[i-1])
    35                 return 0;
    36 
    37         }
    38     }
    39     else
    40         return 0;
    41     return 1;
    42 }
    43 int main()
    44 {
    45     char s1[10002][16],s2[16];
    46     int i;
    47     for (i = 0;; i ++)
    48     {
    49         scanf("%s",s1[i]);
    50         if (s1[i][0]=='#')
    51             break;
    52     }
    53     int n = i;
    54     for (;;)
    55     {
    56         scanf("%s",s2);
    57         if (s2[0]=='#')
    58             break;
    59         for (i = 0; i < n; i ++)
    60         {
    61             if (!strcmp(s1[i],s2))
    62             {
    63                 printf("%s is correct
    ",s2);
    64                 break;
    65             }
    66         }
    67         if (i < n)
    68             continue;
    69         printf("%s:",s2);
    70         for (i = 0; i < n; i ++)
    71         {
    72             if(deal(s1[i],s2))
    73                 printf(" %s",s1[i]);
    74         }
    75         printf("
    ");
    76     }
    77     return 0;
    78 }
    View Code
  • 相关阅读:
    「LibreOJ β Round #4」子集
    「LibreOJ β Round #4」框架
    「LibreOJ β Round #4」游戏
    [HNOI2008]GT考试
    [HNOI2008]水平可见直线
    UVA 1650 Number String
    [USACO14JAN]Recording the Moolympics
    UVA 1390 Interconnect
    UVA 12520 Square Garden
    [HNOI2008]神奇的国度
  • 原文地址:https://www.cnblogs.com/lahblogs/p/3267919.html
Copyright © 2011-2022 走看看