- Translate each rule into one explicit state update.
- Maintain the invariant after every processed item.
- Return the accumulated state once all relevant input has been handled.
Code notes
- 57 lines of Java from the credited upstream file ccc06s1.java.
- The implementation visibly relies on sequence storage.
- 2 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.
Use this to learn the idea, then write your own version.
1234567 8import java.awt.*;9import hsa.*;10 11public class CCC2006s1Maternity12{13 static Console c;14 15 public static void main (String[] args)16 {17 c = new Console ();18 TextInputFile f = new TextInputFile ("s1.3.in");19 int x;20 String mother, father, baby;21 22 mother = f.readString ();23 father = f.readString ();24 x = f.readInt ();25 for (int i = 1 ; i <= x ; i++)26 {27 baby = f.readString ();28 if (possibleBaby (mother, father, baby))29 c.println ("Possible baby.");30 else31 c.println ("Not their baby!");32 }33 }34 35 36 37 38 public static boolean possibleBaby (String m, String f, String b)39 {40 boolean okay = true;41 for (int i = 0 ; i <= 4 && okay ; i++)42 if (b.charAt (i) >= 'A' && b.charAt (i) <= 'E')43 okay = (m.charAt (i * 2) >= 'A' && m.charAt (i * 2) <= 'E') ||44 (m.charAt (i * 2 + 1) >= 'A' && m.charAt (i * 2 + 1) <= 'E') ||45 (f.charAt (i * 2) >= 'A' && f.charAt (i * 2) <= 'E') ||46 (f.charAt (i * 2 + 1) >= 'A' && f.charAt (i * 2 + 1) <= 'E');47 else48 okay = ((m.charAt (i * 2) >= 'a' && m.charAt (i * 2) <= 'e') ||49 (m.charAt (i * 2 + 1) >= 'a' && m.charAt (i * 2 + 1) <= 'e')) &&50 ((f.charAt (i * 2) >= 'a' && f.charAt (i * 2) <= 'e') ||51 (f.charAt (i * 2 + 1) >= 'a' && f.charAt (i * 2 + 1) <= 'e'));52 return okay;53 }54}55 56 57