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