• 微软2014实习生及秋令营技术类职位在线测试-题目4 : Most Frequent Logs


    这一题的题目有点问题,我觉得应该把b) format strings have edit distance  5 of each other变成larger than 5.

     1 #include<stdio.h>
     2 #define M 100
     3 /*
     4 * 采用动态规划算法,
     5 * 详见http://en.wikipedia.org/wiki/Edit_distance 
     6 */
     7 int wfEditDistance(char * a, char * b)
     8 {
     9     int i=0,j=0;
    10     int dist[257][257];
    11     
    12     for(i=0;i<257;i++)
    13         dist[i][0] = i;
    14     for(j=0;j<257;j++)
    15         dist[0][j] = j;
    16     
    17     i=0;j=0;
    18     while(a[i]!='')
    19     {
    20         j=0;
    21         while(b[j]!='')
    22         {
    23             if(*a == *b)
    24                 dist[i+1][j+1] = dist[i][j];
    25             else
    26             {
    27                 dist[i+1][j+1] = dist[i][j+1]+1<dist[i+1][j] +1? dist[i][j+1]+1:dist[i+1][j] +1;
    28                 dist[i+1][j+1] = dist[i+1][j+1]<dist[i][j]?dist[i+1][j+1]:dist[i][j];
    29             }
    30             j++;
    31         }
    32         i++;
    33     }
    34     return dist[i][j];
    35 }
    36 
    37 int main(void)
    38 {
    39     char format[M][256];//存储模板
    40     char ** f = format;
    41     int cnt[M];//存储相同string的个数
    42     char temp[256];
    43     int i=0,f=0;
    44     for(i=0;i<M;i++)
    45         cnt[i] = -1;
    46     while(gets(temp))
    47     {
    48         //第一个string
    49         if(cnt[0] == -1)
    50         {
    51             *f = temp;
    52             cnt[0] = 1;
    53             continue;
    54         }
    55         f = 0;
    56         //遍历是否已有相同模板产生的string
    57         for(i=0;cnt[i]!=-1 && i<M;i++)
    58         {
    59             if(wfEditDistance(temp,format[i])<=5)
    60             {
    61                 cnt[i]++;
    62                 f=1;
    63                 break;
    64             }
    65         }
    66         //如果没有,则把当前string作为新的模板产生的第一个string
    67         if(f==0)
    68         {
    69             *(f+i) = temp;
    70             cnt[i] = 1;
    71         }
    72     }
    73 
    74 
    75 
    76     return 0;
    77 
    78 }
    声明:没有提交online judge, 不知道程序是否会对,测试的case是可以通过的。
    有问题可以和我联系,bettyting2010#163 dot com
  • 相关阅读:
    UIButton 动态改变文本闪烁问题
    利用GDataXML解析XML文件
    限制键盘只能输入数字
    获得view所在的控制器
    使用Canvas绘制简单的时钟控件
    Spring整合ActiveMq消息队列
    Symmetric Key Encryption DES
    OSPF 高级实验
    OSPF 基础实验
    EIGRP 高级实验
  • 原文地址:https://www.cnblogs.com/echoht/p/3662077.html
Copyright © 2020-2023  润新知