2.c 949 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <assert.h>
  5. #define BUFSZ 1024
  6. char buf[BUFSZ + 1] = {0};
  7. int table[53] = {0};
  8. int decode(char c) {
  9. if (c >= 'A' && c <= 'Z') {
  10. return 26 + c - 'A' + 1;
  11. }
  12. if (c >='a' && c <= 'z') {
  13. return c - 'a' + 1;
  14. }
  15. return -1;
  16. }
  17. int process(char *buf, int len, int n) {
  18. if (n == 1) memset(table, 0, 53 * sizeof(int));
  19. for (int i = 0; i < len; i++) {
  20. int code = decode(buf[i]);
  21. if (table[code] == n - 1) {
  22. if (n == 3) return code;
  23. table[code] = n;
  24. }
  25. }
  26. return 0;
  27. }
  28. int main() {
  29. FILE *fp = fopen("input", "r");
  30. int sum = 0;
  31. if (fp == NULL) return -1;
  32. int line = 0;
  33. while (fgets(buf, BUFSZ, fp)) {
  34. if (strlen(buf) <= 1) continue;
  35. int len = strlen(buf) - 1;
  36. sum += process(buf, len, line % 3 + 1);
  37. line++;
  38. }
  39. printf("%d\n", sum);
  40. }