Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- void permut(int , int , int* , char** , int* );
- void ret(int* , int , int* , char** , int* );
- int main(void) {
- int n, j = 0, max = 0;
- scanf("%d", &n);
- char *words[n];
- char *get = malloc(3 * sizeof(char));
- fgets(get, 3, stdin);
- free(get);
- int len[n];
- for (int i = 0; i < n; i++) {
- words[i] = malloc(22 * sizeof(char));
- words[i] = fgets(words[i], 22, stdin);
- len[i] = strlen(words[i]) - 1;
- }
- int res[n];
- for (int i = 0; i < n; i++) {
- res[i] = -1;
- }
- permut(0, n, len, words, res);
- while (res[max] != -1) {
- max++;
- }
- printf("%d\n", max);
- // for (int i = 0; i < 10; i++) {
- // if (res[i])
- // fputs(words[res[i]], stdout);
- // }
- for (int i = 0; i < n; i++) {
- free(words[i]);
- }
- return 0;
- }
- void permut(int i, int n, int *len, char **words, int *res) {
- static int check[10];
- static int once[10] = {0};
- if (i == n) {
- ret(check, n, len, words, res);
- return;
- }
- for (int j = 0; j < n; j++) {
- if (once[j]) {
- continue;
- }
- check[i] = j;
- once[j] = 1;
- permut(i + 1, n, len, words, res);
- once[j] = 0;
- }
- }
- void ret(int *check, int n, int *len, char **words, int *res) {
- static int max = 0;
- static int right[10];
- int imax = 1, count = 0;
- for (int i = 0; i < n; i++)
- printf("%d ", check[i]);
- printf("\n");
- while (imax < n) {
- if (words[check[imax - 1]][0] == words[check[imax]][len[check[imax]] - 1])
- imax++;
- else
- break;
- }
- printf("%d\n", imax);
- if (imax > max) {
- for (int i = 0; i < 10; i++) {
- right[check[i]] = 1;
- res[i] = 0;
- }
- res[0] = check[0];
- right[check[0]] = 0;
- max = imax;
- }
- if ((imax == max) && (imax != 0)) {
- if (right[imax]) {
- res[check[imax]] = 1;
- right[imax] = 0;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement