Find the compressed string in c prog

Find the compressed string


Level: Medium

You are given a collection of words, say as in a dictionary. You can represent it in the following compressed form: the first word will be followed by a sequence of a pair of number and a word. The number in the pair is the position till which the previous words' characters are included in the new word and the tail is the remaining trailing word which is the different than the previous word.

Example:
Suppose successive words in our dictionary are:

color
comma
commatose
dot

Then we can compress it in the following way:
color
2 mma     (to denote that first two characters are same as that of 'color' and remaining string is 'mma')
5 tose      (to denote that the first five characters are same as that of 'comma' and remaining string is 'tose')
0 dot        (to denote that zero characters are same as that of 'commatose' and the remaining string is 'dot')

Input Format:

First line contains the integer 'n' denoting the number of words in the dictionary s.t. 1 <= n <= 1,000
Second line would contain the first word.
It will be followed by 'n-1' lines each containing an integer and a trailing string.
Note: The input is designed such that the integer will always be <= size of previous word formed
Example Input: 
4
zebra
3 u
2 nith
1 ggurat


Output Format: 
Output a single string that is the last resulting word of the given dictionary
Example Output:
zggurat

Explanation:
The dictionary actually is:
zebra
zebu      (3 first characters are common with zebra)
zenith    (2 first characters are common with zebu)
zggurat  (1 first character is common with zenith)

InputOutput
Test Case 1
4
zebra
3 u
2 nith
1 iggurat
ziggurat
Test Case 2
3
cool
4 er
2 mmon-man
common-man
Test Case 3
1
batman
batman
Test Case 4
10
apple
2 titude
0 bail
3 t
2 hool
1 hoopla
4 newmist
5 oliberstein
2 diem
6 mon
bhdiemmon
Test Case 5
10
moonwalk
7 vfsetzqwuhskb
6 tpcu
4 c
0 cbeiusyxj
7 psrtzgky
0 efzipsko
3 wy
1 l
1 fqnhgfi
efqnhgfi
Test Case 6
100
numismatic
4 qenarvwwdoxwe
1 orlx
0 gtwkuwpuk
5 afncdsjlvts
5 yvnazxhxwm
8 yyv
0 vvbgiwwzxk
2 zqdjmesk
8 sswst
2 uptpvoqgyxk
3 oykyiyejf
9 zgazbxgiow
9 tgskpeomy
0 kh
1 fffnwyfxwkl
6 qyyyi
5 bna
0 roabn
2 atxaasdvn
7 udxmrx
2 mj
1 sujhmdvttldmmk
0 hxpunh
5 fyuakn
6 qdzcoygommz
4 atsrwrqreqqrd
3 cvq
1 yitw
3 milrqnvt
0 smgxam
0 nbzewrl
0 sn
2 jvnumiltnksx
8 yroag
4 qgzdprulkbabj
1 znjxhkon
4 asxkl
2 uosqlpwvmjdkg
3 p
0 qfeksdde
4 jxrh
0 pvjscrxlcp
8 bhvt
1 hkxkjzynpsr
3 bejsrdmgoqh
8 fqcvz
5 mgoiaq
3 zqdc
4 kcccxcwkwnclju
8 zqwds
2 pdzrugtzmdnpo
8 lpxveuprlo
8 xhgecpybcwhhm
2 z
0 iydegboizv
5 bmr
0 vwwrdnbx
0 ihfbpzuhoeab
7 obsdzfakstbwb
8 xeqchqkpvqghrv
3 hc
0 cvfbzhdrxswh
3 rnxhonihtbwur
5 kggsjwengcpj
8 mqicx
4 ivcbk
0 iqlxz
4 not
1 oabgsoeeo
9 cbtoajd
2 brjyglonjkludw
1 c
1 bqcis
0 pllddbpnp
7 tazs
4 pfhwfq
3 tkenatmlnbwkg
7 hlhthxpqx
2 fvv
3 mwdomqd
2 zdy
0 wfqdyxvuxj
7 hlobzyokl
6 loyma
3 jv
2 c
1 t
1 o
1 vmypta
4 ncxqk
3 hcfvzevwgvacxn
7 enzvegnlnofuju
7 xecqhwgw
4 sggymtupoffiky
4 undcjrjsmcsqb
7 sjihyjv
4 joadarnwee
7 fucsmwtwvjkwby
8 fvfwpvdjn
wvmhjoaffvfwpvdjn
Test Case 7
200
kleptomaniac
6 dcad
3 k
0 oapciiocvrj
9 xlft
0 vecrreglymwk
8 shkownf
4 nfqpkpohnjexx
9 jzzpllqnzwwwai
9 irq
2 plympr
4 vrpgiewhgyyul
9 padg
0 wmhihff
5 udhnk
5 zrwtihvtbbzgf
9 kv
1 yewqcmvksxkao
1 utcxdjzwnhoek
0 uwwsuyoopvdvvq
3 dooktiaxhvg
3 cjltljcfq
2 zyivo
1 xzeybxoung
3 hterpypz
6 xdocdlrmx
0 csoenhqauwyx
3 vlatymegwqxzh
9 wnekazvf
2 vjlrjuyhgn
9 orz
0 gdipe
5 oibwt
4 nsenupifo
0 czqffvdicpou
3 ugqzw
5 gvqrfdv
4 oivdcrbo
6 fmjwavomvwc
6 ebqib
1 zjmt
2 lfhy
4 kt
2 wjnhdc
6 oq
0 cwctkwxrym
4 hytsozrgjliz
0 nvbmffvxgudeyz
9 iufqq
1 qq
0 oaetsdrjgubw
1 uivxqpxhwyfl
0 taneb
4 npfdtuswilim
5 pjq
2 rptxcgkbrxj
7 pn
1 figbmspqlj
5 fzkxvdjv
8 dmirtamjccgnkd
4 ms
0 xnxxlwgdbhfwla
5 rhgcl
4 esirgn
2 fej
0 rqrybkfs
1 lxt
3 wq
2 krqpvbqdqmhma
5 gfokk
5 pfcktyp
4 ztxuimsml
2 wkl
3 xcsxjepl
7 iueofppwdlsbh
9 lcgh
0 twkfmo
6 qrmder
1 qlkobgcxfr
1 alhldkeabnqm
6 ts
1 zes
3 enwhvzkdthk
0 voaxazqmlsnwd
7 lreqeftkrlkdq
5 gvlt
1 hrogvrlv
5 uv
1 qryycsdban
8 jkoqbkxkorgz
7 wbjtpjpctlslqb
5 kzzgbfoap
6 mt
1 vfdvnipispis
0 rmscpdsypahjs
4 xivmhdt
1 wvaonuzcq
3 vmxpwqinygo
3 qpndyv
3 qgdoszdoagcnqh
5 vgd
2 lqqpkwsaqsic
0 tesdh
5 gwwjlmun
6 dsmrpe
4 rakundoyxgtyx
9 gxjufmownjpjpi
1 lkuxajch
4 bugzdzvaakvnol
9 heacujg
7 faoemjzaaut
8 kgcjafdng
6 ihfiu
3 zhxtwccibvm
3 wsfyxfwfuum
4 sxetw
4 pqeuvtpxzfldlm
7 uztaq
3 ip
2 w
0 makxrplzme
0 yjxfvzseeoydu
2 wop
3 qdubdimxoaff
4 bgj
0 hc
2 qcywauvnidjfj
8 zqnomdsmwzt
2 xenykjkpsheuak
1 eavwlky
7 ukxlmcbcivbnpj
0 owtadxydfo
6 bbb
1 rrjqq
4 rtamlt
1 sqcalqra
8 tdpvmpfen
5 ovxkgj
6 cbjhjw
3 sdzt
3 sgw
0 s
0 ov
0 rnqaqbn
1 bkzfsvpqv
2 x
0 i
1 nbgco
4 rpzkbbquisyt
2 ur
0 lbpaih
2 mpofmbcspr
6 vcvozqfrvm
5 juqthvx
7 ytumqml
6 sw
2 pozkq
3 lgscln
4 va
1 iccbamgjqzzi
5 thjystpxqocr
0 lsflooyw
5 xwgwczb
2 ucir
0 sbuve
5 rmd
1 sesurmpuaha
3 ywatrteccmgjw
9 hifjcstm
0 nmdgjeu
3 z
0 uehvuqs
1 bztuvwggbyotm
4 iwswln
5 hjrswaiddhz
0 jbkoevtjhhdzy
3 mqdjek
4 hkbbywc
4 hkio
3 qhobzwp
5 bfcfvgdm
8 wimxgfwk
1 vavcirvdf
2 svsdy
0 mgjzykhnedvnx
2 lee
2 okxefdhxtgvd
3 nwuinyioa
6 bdpywodiegtwg
1 ozxlxhsm
7 vefckzsau
3 ulpivhfnfxc
7 qauluczrmj
1 twelwqi
6 gqcrkmgjafasx
3 zeoothu
1 htmbjscixj
3 uccikwto
6 oreqyje
2 wkqlsdvwkdhj
mhwkqlsdvwkdhj

#include <stdio.h>
#include <string.h>

/*
 *--------------------------------------
 * Program to solve the compressed string problem
 *--------------------------------------
 */


/*
 *--------------------------------------------
 * Function to fill the next entry into current[],
 * The previous string is assumed to be in prev[]
 *
 * the string current[] will contain the uncompressed
 * string on return
 *--------------------------------------------
 */
void get_next_entry(char prev[], char current[])
{
 int i;
 int j;
 int overlap;  /* common prefix length between prev
       and current */ 
 char tail[100];  /* suffix of the next string */
 int tail_length; /* length of the suffix of the next
       string */

 scanf("%d",&overlap);
 getchar();  /* discard space */
 gets(tail);
 tail_length = strlen(tail);


 /* First copy the common prefix from prev */
 for(i=0;i<overlap;i++){
  current[i]=prev[i];
 }
     
 /* Now copy the suffix from tail */
 for(j=0;j<tail_length;j++){
  current[i+j]=tail[j];
 }
 current[i+j] = '\0';
 return;
}

int main()
{
 int string_index; /* index of the output string */
 char current[100]; /* current string */
 char prev[100];  /* previous string */
 int i;

 scanf("%d",&string_index);
 getchar();  /* discard newline */
 gets(current);  /* the first string */
 for(i=2;i<=string_index;i++){
  strcpy(prev,current);
  get_next_entry(prev,current);
 }
 puts(current);
}

Labels: ,