Huffman Algorithm (ii) 计算字符权重

/*
 *==========================================================
 * Filename    :  cw.cpp
 * Description :
 *
 * Author      :  RollStone (rs), [email protected]
 * Created     :  10/11/2014 11:02
 * Version     :  1.0
 * Last_Change :  2014-10-11 11:37:33
 * Copyright   :  All Rights Reserved. Copyright(c) 2007-2014
 *==========================================================
 */
#include <stdio.h>
struct cwu
{
	char c;
	int  w;
	cwu()
	{
		c=0,w=0;
	}
};
cwu* count_weight(char *s)
{
	cwu *newCWU=new cwu[256];
	if(!newCWU)
	{
		return NULL;
	}
	char ci;
	cwu *p;
	while((ci=*s++)!=0)
	{
		if(ci==‘,‘||ci==‘.‘)
		{
			continue;
		}
		p=newCWU;
		do
		{
			if(p->c==0)
			{
				p->c=ci;
				p->w++;
				break;
			}
			if(p->c==ci)
			{
				p->w++;
				break;
			}
			p++;
		}while(1);
	}
	return newCWU;
}
void output_info(cwu* pc)
{
	while(pc->c!=0)
	{
		printf("(%c,%d) ",pc->c, pc->w);
		pc++;
	}
	printf("\n");
}
时间: 2024-10-28 10:01:46

Huffman Algorithm (ii) 计算字符权重的相关文章

Coursera Algorithm II PA2 Q2

题意: what is the largest value of k such that there is a k-clustering with spacing at least 3?  That is, how many clusters are needed to ensure that no pair of nodes with all but 2 bits in common get split into different clusters? 即, 海明距离小于等于 2 的所有点要被

Coursera algorithm II PA4

题意: 所给数据中是否有负环? 没有负环的图中所有路径中最短的值 思路: 1. bellmanford 判断负环 2. flodyWarshall 求所有定点的最短路径 细节: 1. bellmanford 算法时间复杂度为 o(n^3), 因为图的使用邻接矩阵存储的,  使用邻接表代码会容易理解些, 引用 wiki 的伪代码 1 2 3 4 5 6 // 步骤2:重复对每一条边进行松弛操作 for i from 1 to size(vertices)-1: for each edge (u,

iOS学习-11. 圆角(小于等于四个)类似气泡和计算字符高度

使用贝塞尔曲线, // 小于四个角 圆角 -(void)setbor{ NSString *str = @" couldn't fit this all in a comment to @lomanf's answer. So I'm adding it as an answer."; //计算字符高度 [Corner layoutHeightWithLable:self.label text:str]; /* 1.使用空白 view addSubView label 2.得到类似 q

1.计算字符的ASCII码

(1)问题:在终端输入一个字符,输出它的ASCII码 (2)分析:一个字符在内存中的存储方式是以它的ASCII码形式存放的,大小为8位:例如:空格符的ASCII码为32,它在内存中32对应的8位二进制数为1000000就代表一个空格. (3)源码: #include "stdio.h" int main(){    char c;    printf("please input a character\n");    scanf("%c",&am

[LeetCode] Nested List Weight Sum II 嵌套链表权重和之二

Given a nested list of integers, return the sum of all integers in the list weighted by their depth. Each element is either an integer, or a list -- whose elements may also be integers or other lists. Different from the previous question where weight

c语言判断是否是utf8字符串,计算字符个数

#include <stdio.h> #include <string.h> #include <stdlib.h> /**************************************************************************** Unicode符号范围 | UTF-8编码方式 (十六进制) | (二进制) 0000 0000-0000 007F:0xxxxxxx 0000 0080-0000 07FF:110xxxxx 10x

Swift计算字符数量

1 通过调用全局 countElements 函数并将字符串作为参数进行传递可以获取该字符串的字符数量. 2 let unusualMenagerie = "Koala ??, Snail ??, Penguin ??, Dromedary ??" 3 println("unusualMenagerie has \(countElements(unusualMenagerie)) characters") // prints "unusualMenager

C语言中,当计算字符数组长度时,用sizeof 和strlen 的原理及两者的区别

字符数组的长度计算:必须以终止符'\0'作为边界,但对字符数组赋值时,有两种方式: 1:定义时用字符初始化 (1)char chs[7] = {'a', 'c', '0', 'z', '3','d'}; // 长度为6 上式等价于: (2)char chs[7] = {'a', 'c', '0', 'z', '3','d', '/0'}; // 长度为6 也等价于: (3)char chs[] = {'a', 'c', '0', 'z', '3','d', '/0' }; // 长度为6 但不等

计算字符个数

1 #include <stdio.h> 2 #include <string.h> 3 int main() 4 { 5 char st[1000]; 6 char ch; 7 int count = 0; 8 memset(st,0,sizeof(st)); 9 gets(st); 10 ch = getchar(); 11 if(ch >='a' && ch <='z') 12 ch = ch -'a'+'A'; 13 for(int i = 0;