Simple prefix compression

Source: Internet
Author: User

Description

Many databases store the data in the character fields (and especially indices) using prefix compression. This technique compresses a sequence of strings A1, ..., an by the following method:if there is strings Ai = ai,1ai,2...ai,p and Ai + 1= ai+1,1ai+1,2...ai+1,q
such that for some J <= min (p, q) ai,1= ai+1,1, ai,2= ai+1,2, ... ai,j= ai+1,j, then the second string is stored as [J]ai +1,j+1ai+1,j+2. Ai+1,q, where [j] is a single character with Code J.

If j = 0, which is, strings does not has any common prefix, then the second string was prefixed with zero byte, and so the Tal length actually increases.

Constraints
1 <= N <= 10000, 1 <= Length (Ai) <= 255.
Input
First line of input contains integer number N, with following N lines containing strings A1 ... An
Output
Output must contain a single integer–minimal total length of compressed strings.
Sample input

3abcatestatext

Sample output

11
Thinking of solving problems

The above topic is probably to write a program to calculate the size of the compressed string, the compression method is the same as the previous string is directly represented by a number, such as the example given, is actually the three string compression bit

abc1test3xt
Code Implementation
#include <stdio.h>#include <stdlib.h>#include <string.h>intMinintAintb) {if(A <= B)returnAElse        returnb;}intMainintargcChar Const*argv[]) {intMin_len =0;intCount =0;intnum =0;intI, j =0;Chara[255], b[255];intn;scanf("%d", &n);scanf('%s ', a);intA_len =strlen(a); Count + = A_len; for(j =0; J < N-1; J + +) {scanf('%s ', b);intB_len =strlen(b);        Min_len = min (A_len, B_len); Count + = B_len; for(i =0; i < Min_len; i++) {if(A[i]! = B[i]) Break; } count = Count-i +1;memcpy(A, B, B_len);    A_len = B_len; }printf("%d\n", count);return 0;}

Simple prefix compression

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.