Solution Idea

50094. Word Learning

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include<stdio.h>
#include<string.h>
int main(void){
    int map[26][26] = {0};
    int check[26] ={0};
    int T;
    scanf("%d", &T);
    char a[100];
    while(scanf("%s",a) != EOF) {
        int l = strlen(a);
        for(int i = 0; i < l-1; i++) {
            map[ a[i] - 'a'][ a[i+1] - 'a' ] ++;
        }
    }
    int max = T;
    int x;
    for(int i = 0; i< 26; i++) {
        for(int j  =0; j< 26; j++){
            if( map[i][j] > max){
                x = i;
                max = map[i][j];
            }
        }
    }
    char new_word[26];
    char length = 0;
    while(1){
        if(check[x] == 1) break;
        check[x]++;
        new_word[length++] = x + 'a';
        new_word[length] = '\0';
        max = T;
        int temp = -1;
        for(int i =0; i<26; i++){
            if(check[i] == 0 && map[x][i] > max){
                temp = i;
                max = map[x][i];
            }
        }
        if(temp == -1) break;
        x = temp;
    }
    for(int i = 0; i< 26; i++) {
        for(int j  =0; j< 26; j++){
            printf("%d",map[i][j]);
            if( j != 25) printf(" ");
        }
        printf("\n");
    }
    printf("%s", new_word);
    return 0;
}

Discussion