#include #include int main (void) { int K, k; char occ[51]; char s[31]; int x, y, x1, x2, m; char f; int nd; int t[51]; FILE *in = fopen ("contest.in", "r"), *out = fopen("contest.out", "w"); fscanf (in, "%d\n", &K); for (k = 1; k <= K; k ++) { fprintf (out, "Data Set %d:\n", k); for (x = 0; x < 51; x ++) occ[x] = -1; nd = 0; x = 0; y = 0; fgets (s, 30, in); s[strlen(s)-1] = 0; while (strcmp (s, "endprogram") != 0) { if (strcmp (s, "basic") == 0) if (y > occ[x]) occ[x] = y; else; else if (strcmp (s, "endloop") == 0) { if (t[nd-1] == 1) x --; else if (t[nd-1] == -1) y --; nd --; } else if (s[5] == 'x') { t[nd] = 1; nd ++; x ++; } else if (s[5] == 'y') { t[nd] = -1; nd ++; y ++; } else { t[nd] = 0; nd ++; } fgets (s, 30, in); s[strlen(s)-1] = 0; } m = -1; for (x = 50; x >= 0; x --) if (occ[x] <= m) occ[x] = -1; else m = occ[x]; for (x1 = 0; x1 < 51; x1 ++) if (occ[x1] >= 0) for (x2 = x1 + 2; x2 < 51; x2 ++) if (occ[x2] >= 0) for (x = x1 + 1; x < x2; x ++) if (((float) occ[x]) <= (float) occ[x1] + ((float)(x-x1)) * ((float)(occ[x2]-occ[x1])) / ((float) (x2-x1))) occ [x] = -1; f = 1; for (x = 50; x >= 0; x --) if (occ[x] >= 0) { if (!f) fprintf (out, " + "); f = 0; if (x == 0 && occ[x] == 0) fprintf (out, "1"); else { if (x == 1) fprintf (out, "x"); else if (x >= 2) fprintf (out, "x^%d", x); if (occ[x] == 1) fprintf (out, "y"); else if (occ[x] >= 2) fprintf (out, "y^%d", occ[x]); } } if (f) fprintf (out, "0"); fprintf (out, "\n\n"); fscanf (in, "\n"); } fclose (in); fclose (out); return 0; }