(Good topic)单词的压缩编码(leetcode3.28每日打卡)
输出: 10
说明: S = "time#bell#" , indexes = [0, 2, 5] 。
1 <= words.length <= 2000
1 <= words[i].length <= 7
每个单词都是小写字母 。
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 }
(Good topic)单词的压缩编码(leetcode3.28每日打卡)的更多相关文章
- Java实现 LeetCode 820 单词的压缩编码(暴力)
820. 单词的压缩编码 给定一个单词列表,我们将这个列表编码成一个索引字符串 S 与一个索引列表 A. 例如,如果这个列表是 ["time", "me", & ...
- Java实现 LeetCode 820 单词的压缩编码(字典树)
820. 单词的压缩编码 给定一个单词列表,我们将这个列表编码成一个索引字符串 S 与一个索引列表 A. 例如,如果这个列表是 ["time", "me", & ...
- leetcode之820. 单词的压缩编码 | python极简实现字典树
题目 给定一个单词列表,我们将这个列表编码成一个索引字符串 S 与一个索引列表 A. 例如,如果这个列表是 ["time", "me", "bell& ...
- 2018/1/28 每日一学 单源最短路的SPFA算法以及其他三大最短路算法比较总结
刚刚AC的pj普及组第四题就是一种单源最短路. 我们知道当一个图存在负权边时像Dijkstra等算法便无法实现: 而Bellman-Ford算法的复杂度又过高O(V*E),SPFA算法便派上用场了. ...
- [Swift]LeetCode820. 单词的压缩编码 | Short Encoding of Words
Given a list of words, we may encode it by writing a reference string S and a list of indexes A. For ...
- python set() leetcode 签到820. 单词的压缩编码
题目 给定一个单词列表,我们将这个列表编码成一个索引字符串 S 与一个索引列表 A. 例如,如果这个列表是 ["time", "me", "bell& ...
- 【LeetCode】820. 单词的压缩编码 Short Encoding of Words(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode-cn.com/problems/short- ...
- 2018/03/28 每日一个Linux命令 之 mkdir/rmdir
用于建立空文件夹和删除文件夹 -- 两命令重要参数 -p 递归建立/删除 -- 例如 mkdir -p demo1/demo2/demo3 建立demo3空文件夹,如果demo1/demo2没建立也建 ...
- P1664 每日打卡心情好
题目背景 在洛谷中,打卡不只是一个简单的鼠标点击动作,通过每天在洛谷打卡,可以清晰地记录下自己在洛谷学习的足迹.通过每天打卡,来不断地暗示自己:我又在洛谷学习了一天,进而帮助自己培养恒心.耐心.细心. ...
- (leetcode每日打卡)秋叶收藏集【动态规划】
LCP 19.秋叶收藏集 题目链接 算法 动态规划 时间复杂度O(n) 1.题目要求最终形成[红.黄.红]三部分,每部分数量可以不相等,问最终调整操作数量最小是多少.这道题一开始考虑暴力去做,枚举两个 ...
随机推荐
- word中查找替换不能使用 解决方案
打开查找,然后点更多,最下面点不限定格式
- cesium中文网
http://cesium.xin/cesium/cn/Documentation1.62/CallbackProperty.html
- [nginx]编译安装openresty
前言 OpenResty是一个基于Nginx和Lua的高性能Web平台,其内部集成了大量精良的Lua库.第三方模块以及大多数的依赖项.用于方便地搭建能够处理超高并发.扩展性极高的动态 Web 应用.W ...
- Python生成30万条Excel 测试数据
使用Python生成30万条Excel 测试数据 from openpyxl import Workbook from concurrent.futures import ThreadPoolExec ...
- Django视图与网址进阶
1. 采用/add/?a=4&b=5这样get方法进行 1)修改learn/views.py文件 代码: #增加新函数 def add(request): a=request.GET['a'] ...
- 6、Spring之基于xml的自动装配
6.1.场景模拟 6.1.1.创建UserDao接口及实现类 package org.rain.spring.dao; /** * @author liaojy * @date 2023/8/5 - ...
- [ABC140E] Second Sum
2023-02-13 题目 题目传送门 翻译 翻译 难度&重要性(1~10):4 题目来源 AtCoder 题目算法 双向链表 解题思路 \(1.\) 当我们用从小到大的顺序来求解时,把原来求 ...
- 【升职加薪秘籍】我在服务监控方面的实践(7)-业务维度的redis监控
大家好,我是蓝胖子,关于性能分析的视频和文章我也大大小小出了有一二十篇了,算是已经有了一个系列,之前的代码已经上传到github.com/HobbyBear/performance-analyze,接 ...
- 《小白WEB安全入门》03. 漏洞篇
@ 目录 SQL注入和简单绕过原理 什么是SQL 什么是SQL注入 XSS漏洞原理 什么是XSS XSS分类 NOSQL注入 什么是NOSQL CSRF原理 什么是CSRF 网络摄像头入侵原理 什么是 ...
- SQL select关联表查询 统计另一个表合计
db_order 是记录订单的, 一个订单一条记录.(oid, 运费, 实收金额, 产品KEY.......) db_soid 是记录出售商品的 (id, 商品名称, 售价, 数量, 成本, 标识 ...