- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这几天因为做题一直在贴一些代码,最后好像结束了,但是发现不行。练习要求输入:- N 一个整数,代表要读取的字符串数- K 一个整数- N 个字符串字符串可以重复。在输出中有最频繁的 K 个字符串的打印,根据它们的频率(降序)排序。
示例测试集:
输入:
6
2
mickey
mouse
mickey
hello
mouse
mickey
输出:
mickey // Has freq 3
mouse // Has freq 2
我希望我能很好地解释这个练习,因为这是我的尝试。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct _stringa {
char* string;
int freq;
} stringa;
int compare(const void *elem1, const void *elem2) {
stringa *first = (stringa *)elem1;
stringa *second = (stringa *)elem2;
if (first->freq < second->freq) {
return -1;
} else if (first->freq > second->freq) {
return 1;
} else {
return 0;
}
}
int BinarySearch(stringa** array, char* string, int left, int right) {
int middle;
if (left==right) {
if (strcmp(string,array[left]->string)==0) {
return left;
} else {
return -1;
}
}
middle = (left+right)/2;
if ((strcmp(string,array[middle]->string)<0) || (strcmp(string,array[middle]->string)==0) ) {
return BinarySearch(array, string, left, middle);
} else {
return BinarySearch(array, string, middle+1, right);
}
}
int main (void)
{
char value[101];
int n = 0;
int stop;
scanf("%d", &n); // Number of strings
scanf("%d", &stop); // number of the most frequent strings to print
stringa **array = NULL;
array = malloc ( n * sizeof (struct _stringa *) );
int i = 0;
for (i=0; i<n; i++) {
array[i] = malloc (sizeof (struct _stringa));
array[i]->string = malloc (sizeof (value));
scanf("%s", value);
int already;
already = BinarySearch(array, value, 0, i); // With a binary search, I see if the string is present in the previous positions of the array I am occupying. If it is not present, I copy the string into the array, otherwise, I use the value of binary search (which is the position of the element in the array) and I update the frequency field
if (already==-1) {
strcpy(array[i]->string,value);
array[i]->freq = 1;
} else {
array[already]->freq += 1;
}
}
stringa **newarray = NULL; // New struct array of strings
newarray = malloc ( n * sizeof (struct _stringa *) );
int k = 0;
for (i=0; i<n; i++) { // I use this loop to copy the element that don't have a frequency == 0
if (array[i]->freq != 0) {
newarray[k] = malloc(sizeof(struct _stringa));
newarray[k] = malloc(sizeof(value));
newarray[k]->string = array[i]->string;
newarray[k]->freq = array[i]->freq;
k++;
}
}
qsort(newarray, n, sizeof(stringa*), compare);
i=0;
while ((newarray[i]!= NULL) && (i<k)) {
printf("%s ", newarray[i]->string);
printf("%d\n", newarray[i]->freq);
i++;
}
// Freeing operations
while (--n >= 0) {
if (array[n]->string) free (array[n]->string);
if (array[n]) free (array[n]);
}
if (array) free (array);
if (newarray) free (newarray);
return 0;
}
提前感谢所有有时间和耐心阅读此代码的人。
编辑:
我忘了添加它不能正常工作的地方。如果出于调试原因我不使用 qsort,并且我使用此输入为例:5个2//随机数,我仍然需要执行“打印 k 个字符串”部分,你好你好你好你好你好
它打印:你好 3(频率)你好 2(频率)
所以它不能正常工作。正如您在评论中所建议的那样,二进制搜索存在缺陷,因为它仅适用于有序列表。我能做的是每次都订购阵列,但我认为这会适得其反。摆脱仅定位数组中不存在的字符串的问题的想法是什么?
最佳答案
如果你想要一种无需排序的高效方法,请使用哈希表。否则,只需将每个唯一字符串放入一个数组中并对其进行线性扫描,简单可靠。
在现代硬件上,由于缓存和最小化间接寻址,这种扫描实际上速度很快。对于少量项目,插入排序实际上比 qsort 在实践中更有效。以“Tim sort”算法为例,该算法稳定且避免了qsort对接近排序数据的不良性能,它混合了合并和插入排序以实现n Log n,在真实数据上没有极端情况。
关于C - 打印出现次数最多的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24594614/
你好,我有一张 table : from | to | item | count ------- Jack | Danie| food | 10 Danie| Maria| food | 2 Ja
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 这个问题似乎偏离主题,因为它缺乏足够的信息来诊断问题。 更详细地描述您的问题或 include a mini
我正在尝试解决以下面试问题 Given two arrays firstDay and lastDay representing the intervals in days of possible m
这个问题已经有答案了: Explanation of a output of a C program involving fork() (2 个回答) 已关闭 9 年前。 这是我从我的研究所去年的试卷
如何在 html 页面上重复一个 div X 次,可以说我想设置方差来声明重复次数。重复这个部分 5 次,我假设它是用 JS 的。 black BLUE WHITE strip 我
我目前使用类中的函数将数据插入数据库,如果每行成功插入(从 csv 文件),则会记录一条消息(logMessage 函数),以显示哪一行成功或失败。但是我想要已导入数据库的成功执行的计数。我遇到了一些
这个问题可能看起来非常基础,但我很难弄清楚如何做。我有一个整数,我需要使用 for 循环来循环整数次。 首先,我尝试了—— fn main() { let number = 10; // An
我正在准备 CS 125 期末考试,其中(简要地)介绍了 Big O Notation。 鉴于: Mergesort 的最佳运行时间为 O(N lg(N)),最坏运行时间为 O(N lg (N)) 有
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 3 年前。 Improve this qu
我正在构建一个简单的程序来计算骰子实验中数字的频率,但我尝试扩展它并将最大 throw 次数增加到巨大的数字,通过反复试验,我发现最大限制为519253。 使用这个最大值,我也无法创建任何新数组,它会
这是一道面试题 There is an airline company that wants to provide new updates to all of its flight attendant
我正在尝试以一种可以节省我无数小时的繁琐数据输入的方式实现 Excel 自动化。这是我的问题。 我们需要为所有库存打印条形码,其中包括 4,000 种型号,每种型号都有特定数量。 Shopify是我们
我想根据给定的预定义级别(从级别 1 到级别 6)分离代码中的所有内容,现在我的 JSON 读取 $scope.myJson=[{ id: 1, level: 1, name: "any
我创建了一个菜单,它使用一些 CSS 和 jquery 在悬停时显示其子菜单。事情是,如果用户在菜单项上多次悬停,它会有点滑稽。这是网址:http://91.202.168.37/~ibi/ ,这是
假设我对每小时的事件数进行了如下统计: np.random.seed(42) idx = pd.date_range('2017-01-01', '2017-01-14', freq='1H') df
我想确保我正确理解了这个概念: 在 Hadoop 权威指南中指出:“设计文件系统的目标始终是减少与要传输的数据量相比的查找次数。”在此声明中,作者指的是 Hadoop 逻辑 block 的“seeks
我有一个用 C++11 编写的程序,我想计算 std::vector 的 move 和复制(构造和赋值)次数。对象。有办法吗? 最好的问候 最佳答案 否。 std::vector<>的执行没有办法做到
我们组织的帐户空间不足,我们一直在尝试剔除一些较旧的存储库。问题在于一些较旧的存储库可能仍然是事件服务的依赖项(即使它们多年未更新)。 我知道我们可以跟踪克隆,但据我所知,我们看不到直接下载/pull
我是一名优秀的程序员,十分优秀!