fork download
  1. #include <inttypes.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5.  
  6.  
  7. // return length of common prefix of a, b zero-terminated strings
  8. size_t
  9. string_similarity(char* a, char* b) {
  10. size_t n = 0;
  11. while (*a && *b && *a++ == *b++) ++n;
  12. return n;
  13. }
  14.  
  15. int main() {
  16. int n = 0;
  17. if (fscanf(stdin, "%d\n", &n) != 1) { // read number of tests
  18. perror("fscanf");
  19. exit(EXIT_FAILURE);
  20. }
  21.  
  22.  
  23. const int maxsize = 100002; // + \n \0
  24. char s[maxsize];
  25. for (int i = 0; i < n; ++i) {
  26. if (!fgets(s, maxsize, stdin)) { // read a string to test
  27. perror("fgets");
  28. exit(EXIT_FAILURE);
  29. }
  30. size_t len = strlen(s);
  31. if (s[len-1] == '\n') { // chop newline
  32. s[len-1] = '\0';
  33. --len;
  34. }
  35. uint64_t sum = len; // to support maxsize*(maxsize-1)/2
  36. for (char* suff = s+1; *suff; ++suff) { // for all suffixes
  37. sum += string_similarity(suff, s);
  38. }
  39. printf("%" PRIu64 "\n", sum);
  40. }
  41. return 0;
  42. }
  43.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
2
ababaa
aa
compilation info
prog.c: In function ‘main’:
prog.c:25: error: ‘for’ loop initial declaration used outside C99 mode
prog.c:36: error: ‘for’ loop initial declaration used outside C99 mode
stdout
Standard output is empty