• (Good topic)单词的压缩编码(leetcode3.28每日打卡)


    给定一个单词列表,我们将这个列表编码成一个索引字符串 S 与一个索引列表 A。
    例如,如果这个列表是 ["time", "me", "bell"],我们就可以将其表示为 S = "time#bell#" 和 indexes = [0, 2, 5]。
    对于每一个索引,我们可以通过从字符串 S 中索引的位置开始读取字符串,直到 "#" 结束,来恢复我们之前的单词列表。
    那么成功对给定单词列表进行编码的最小字符串长度是多少呢?
     
    示例:
    输入: words = ["time", "me", "bell"]
    输出: 10
    说明: S = "time#bell#" , indexes = [0, 2, 5] 。

     
    提示:

     1 <= words.length <= 2000
     1 <= words[i].length <= 7
     每个单词都是小写字母
     
    思路:先把每个字符串都逆序,然后再根据字典序排序,假如“time”,“lime”,“hell”,“sometime”,“shell”,“me”。
    逆序后:“emit”,“emil”,“lleh”,“emitemos”,“llehs”,“em”
    按照字典序排序:“em”,“emil”,“emit”,“emitemos”,“lleh”,“llehs”
    然后只需要判断当前是否为后一个单词的前缀(红色的是后一个单词的前缀),如果是前缀则减去当前单词的长度,以及一个‘#’。
     
    通过这道题学习到的东西:
    1.字符串只能在定义的时候可以直接赋值,例如:char *s = “time”  或者char s[10] = "time",不能定义完后再用“=”赋值,如果这样得到字符串是只读,不能修改,需要用到strcpy
    strcpy(str1,“time”),把time赋值给str1,并且自带'';
    2.判断前缀可以用strncmp函数
    int strncmp ( const char * str1, const char * str2, size_t n ),比较前n个字节,如果前n个字符相等,则返回0,如果str1>str2则返回大于0的值,否则返回小于0的值。
    3.主函数中的二维数组只能用指针传到被调用的函数中,即char **a,才行,char[n][n]不行。
    4.c语言qsort与sort函数都可排成字典序。
    5.calloc函数可以申请空间同时将数组初始化为0
     1 #include<stdio.h>
     2 #include<string.h>
     3 #include<stdlib.h>
     4 
     5 int Cmp(const void* a, const void* b)
     6 {
     7     char* s1 = *(char**)a;
     8     char* s2 = *(char**)b;
     9 
    10     return strcmp(s1, s2);
    11 }
    12 
    13 int minimumLengthEncoding(char** words, int wordsSize)
    14 {
    15     if (words == NULL || wordsSize == 0)
    16     {
    17         return 0;
    18     }
    19     int i, j,res = 0;
    20     int totalLen = 0;
    21     int* size = (int*)calloc(wordsSize, sizeof(int));
    22     char t[9] = { 0 };
    23 
    24     for (i = 0; i < wordsSize; i++)  //每个字符串逆序
    25     {
    26         int len = strlen(words[i]);
    27         for (j = 0; j < len / 2; j++)
    28         {
    29             char t = words[i][j];
    30             words[i][j] = words[i][len-j-1];
    31             words[i][len-j-1] = t;
    32         }
    33     }
    34     qsort(words, wordsSize, sizeof(char*), Cmp); //字符串字典序排序    
    35     /*for (i = 0; i < wordsSize-1; i++)  //冒泡排序
    36     {
    37         for (j = 0; j < wordsSize - 1 - i; j++)
    38         {
    39             if (strcmp(words[j], words[j + 1]) > 0)
    40             {
    41                 strcpy(t, words[j]);
    42                 strcpy(words[j], words[j + 1]);
    43                 strcpy(words[j + 1], t);
    44             }
    45         }
    46     }*/
    47 
    48     for (i = 0; i < wordsSize; i++)  //总长度,并记录每个串个长度
    49     {
    50         size[i] = strlen(words[i]);
    51         totalLen += size[i];
    52     }
    53     totalLen += wordsSize;  //加所有的'#'
    54 
    55     for (i = 0; i < wordsSize - 1; i++)
    56     {
    57         if (strncmp(words[i], words[i + 1], size[i]) == 0)
    58         {
    59             totalLen -= (size[i] + 1);
    60         }
    61     }
    62     free(size);
    63     return totalLen;
    64 }
    65 
    66 int main(void)
    67 {
    68     char** s;
    69     int num;
    70     s = (char **)malloc(sizeof(char*) * 6);
    71     for (int i = 0; i < 6; i++)
    72     {
    73         s[i] = (char *)malloc(sizeof(char) * 10);
    74     }
    75     strcpy(s[0], "time");
    76     strcpy(s[1], "lime");
    77     strcpy(s[2], "hell");
    78     strcpy(s[3], "sometime");
    79     strcpy(s[4], "shell");
    80     strcpy(s[5], "me");
    81 
    82     num = minimumLengthEncoding(s, 6);
    83     printf("%d", num);
    84     return 0;
    85 }
     
     
  • 相关阅读:
    python入门
    二级域名分发
    检测网站日常运行
    为自己的网站添加天气显示
    基于linux下LNMP环境设置wordpress伪静态
    个人博客
    静态页跨页面传值
    微信小程序制作-随笔4
    中间带文字的分割线
    微信小程序制作-随笔3
  • 原文地址:https://www.cnblogs.com/ZhengLijie/p/12586590.html
Copyright © 2020-2023  润新知