Advertisement
Guest User

Untitled

a guest
Nov 20th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.11 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. void permut(int , int , int* , char** , int* );
  5. void ret(int* , int , int* , char** , int* );
  6. int main(void) {
  7. int n, j = 0, max = 0;
  8. scanf("%d", &n);
  9. char *words[n];
  10. char *get = malloc(3 * sizeof(char));
  11. fgets(get, 3, stdin);
  12. free(get);
  13. int len[n];
  14. for (int i = 0; i < n; i++) {
  15. words[i] = malloc(22 * sizeof(char));
  16. words[i] = fgets(words[i], 22, stdin);
  17. len[i] = strlen(words[i]) - 1;
  18. }
  19. int res[n];
  20. for (int i = 0; i < n; i++) {
  21. res[i] = -1;
  22. }
  23. permut(0, n, len, words, res);
  24. while (res[max] != -1) {
  25. max++;
  26. }
  27. printf("%d\n", max);
  28. // for (int i = 0; i < 10; i++) {
  29. // if (res[i])
  30. // fputs(words[res[i]], stdout);
  31. // }
  32. for (int i = 0; i < n; i++) {
  33. free(words[i]);
  34. }
  35. return 0;
  36. }
  37.  
  38. void permut(int i, int n, int *len, char **words, int *res) {
  39. static int check[10];
  40. static int once[10] = {0};
  41. if (i == n) {
  42. ret(check, n, len, words, res);
  43. return;
  44. }
  45. for (int j = 0; j < n; j++) {
  46. if (once[j]) {
  47. continue;
  48. }
  49. check[i] = j;
  50. once[j] = 1;
  51. permut(i + 1, n, len, words, res);
  52. once[j] = 0;
  53. }
  54. }
  55. void ret(int *check, int n, int *len, char **words, int *res) {
  56. static int max = 0;
  57. static int right[10];
  58. int imax = 1, count = 0;
  59. for (int i = 0; i < n; i++)
  60. printf("%d ", check[i]);
  61. printf("\n");
  62. while (imax < n) {
  63. if (words[check[imax - 1]][0] == words[check[imax]][len[check[imax]] - 1])
  64. imax++;
  65. else
  66. break;
  67. }
  68. printf("%d\n", imax);
  69. if (imax > max) {
  70. for (int i = 0; i < 10; i++) {
  71. right[check[i]] = 1;
  72. res[i] = 0;
  73. }
  74. res[0] = check[0];
  75. right[check[0]] = 0;
  76. max = imax;
  77. }
  78. if ((imax == max) && (imax != 0)) {
  79. if (right[imax]) {
  80. res[check[imax]] = 1;
  81. right[imax] = 0;
  82. }
  83. }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement