Problem solution · Turing

CCC 1997 P4 - Dynamic Dictionary Coding

CCC 1997 P4 - Dynamic Dictionary Coding: a Turing solution using direct simulation. Learn the idea, check the complexity, and read the full code, with credit to CCCSolutions.

Technique
Direct simulation
Source
CCCSolutions
Length
72 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Direct simulation

For CCC 1997 P4 - Dynamic Dictionary Coding, the implementation follows the problem’s operations directly while maintaining only the state needed for the next decision.

  1. Translate each rule into one explicit state update.
  2. Maintain the invariant after every processed item.
  3. Return the accumulated state once all relevant input has been handled.

Code notes

  • 72 lines of Turing from the credited upstream file ccc97s4.t.
  • The implementation keeps its working state in language-native values and containers.
  • No explicit loop blocks detected.

Complexity

Count the number and nesting of passes over the input, then include the maintained containers in the memory estimate.

Check the problem constraints before deciding whether this complexity will pass.

Source

Code and credit

This code comes from CCCSolutions by CCCSolutions contributors · Milliken Mills High School and is used under the MIT licence.

Full codeCCC 1997 P4 - Dynamic Dictionary Coding · TuringTuring
Use this to learn the idea, then write your own version.
% CCC 1997% problem D: Dynamic Dictionary Coding%% compress text, replacing duplicate words with position in a dictionary% of the words. Non duplicates are put at end of dictionary.% Size of text indicates a simple array and linear search techniques are% good enough. % file handling is used, the number of text sets is given% then the text sets, separated by a blank line  var infile : string := "ddc.in"var outfile : string := "ddc.out"var fi, fo : intvar n, dn, x, k : intvar d : array 1 .. 5000 of stringvar line, word : string open : fi, infile, getopen : fo, outfile, put get : fi, nfor i : 1 .. n    dn := 0     % get lines of text    loop        get : fi, line : *        exit when line = ""         % get words of line        loop            exit when line = ""            x := index (line, " ")            if x = 0 then                word := line                line := ""            else                word := line (1 .. x - 1)                line := line (x + 1 .. *)            end if             %linear search for word            k := 1            loop                exit when k > dn or d (k) = word                k := k + 1            end loop             %print the word or location of word            if k < dn then                put : fo, k, " " ..            else                dn := dn + 1                d (dn) := word                put : fo, word, " " ..            end if        end loop        put : fo, ""    end loop    put : fo, ""end for close : ficlose : fo      

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗