Codeforces Round #390 (Div. 2), problem: (C) Vladik and Chat Solution in C/C++

Codeforces Round #390 (Div. 2), problem: (C) Vladik and Chat Solution in C/C++

#include <stdio.h>
#include <string.h>

int main()
{
    int t;
    scanf("%d", &t);
    while(t--) {
        int n, m;
        scanf("%d", &n);
        char Name[100][11];
        for(int i = 0; i < n; ++i) {
            scanf("%s", Name[i]);
        }
        scanf("%d\n", &m);
        char Msg[100][132];
        char Flag[100][100];
        int Cnt[100];
        int Ans[100];
        memset(Ans, -1, sizeof(Ans));
        memset(Cnt, 0, sizeof(Cnt));
        memset(Flag, 0, sizeof(Flag));
        for(int i = 0; i < m; ++i) {
            fgets(Msg[i], 131, stdin);
            if(Msg[i][0] == '?') {
                Cnt[i] = n;
                int start = 1;
                int end;
                while(Msg[i][start] != '\0') {
                    if((Msg[i][start] >= 'a' && Msg[i][start] <= 'z') || (Msg[i][start] >= 'A' && Msg[i][start] <= 'Z') || (Msg[i][start] >= '0' && Msg[i][start] <= '9')) {
                        int end = start;
                        while(Msg[i][end] != '\0') {
                            if((Msg[i][end] >= 'a' && Msg[i][end] <= 'z') || (Msg[i][end] >= 'A' && Msg[i][end] <= 'Z') || (Msg[i][end] >= '0' && Msg[i][end] <= '9')) {
                                ++end;
                            } else {
                                break;
                            }
                        }
                        char subStr[101];
                        int len = 0;
                        for(int j = start; j < end; ++j) {
                            subStr[len++] = Msg[i][j];
                        }
                        subStr[len] = '\0';
                        for(int j = 0; j < n; ++j) {
                            if(strcmp(subStr, Name[j]) == 0) {
                                if(Flag[i][j] == 0) {
                                    Flag[i][j] = -1;
                                    --Cnt[i];
                                    break;
                                }
                            }
                        }
                        start = end;
                    } else {
                        ++start;
                    }
                }
                if(Cnt[i] == 1) {
                    for(int j = 0; j < n; ++j) {
                        if(Flag[i][j] == 0) {
                            Ans[i] = j;
                            break;
                        }
                    }
                }
            } else {
                memset(Flag[i], -1, 100 * sizeof(char));
                char curName[11];
                int len = 0;
                for(int j = 0; Msg[i][j] != ':'; ++j) {
                    curName[len++] = Msg[i][j];
                }
                curName[len] = '\0';
                Cnt[i] = 1;
                for(int j = 0; j < n; ++j) {
                    if(strcmp(curName, Name[j]) == 0) {
                        Flag[i][j] = 0;
                        Ans[i] = j;
                        break;
                    }
                }
            }
        }
        for(int i = 0; i < m; ++i) {
            if(Cnt[i] == 1) {
                int cur = i;
                while(cur - 1 >= 0 && Flag[cur - 1][Ans[cur]] == 0) {
                    Flag[cur - 1][Ans[cur]] = -1;
                    if(--Cnt[cur - 1] != 1) {
                        break;
                    }
                    for(int j = 0; j < n; ++j) {
                        if(Flag[cur - 1][j] == 0) {
                            Ans[cur - 1] = j;
                            break;
                        }
                    }
                    --cur;
                }
                cur = i;
                while(cur + 1 < m && Flag[cur + 1][Ans[cur]] == 0) {
                    Flag[cur + 1][Ans[cur]] = -1;
                    if(--Cnt[cur + 1] != 1) {
                        break;
                    }
                    for(int j = 0; j < n; ++j) {
                        if(Flag[cur + 1][j] == 0) {
                            Ans[cur + 1] = j;
                            break;
                        }
                    }
                    ++cur;
                }
            }
        }
        char Impossible = 0;
        for(int i = 0; i < m; ++i) {
            if(Cnt[i] == 0) {
                Impossible = 1;
            }
        }
        if(Impossible) {
            puts("Impossible");
        } else {
            for(int i = 0; i < m; ++i) {
                if(Cnt[i] != 1) {
                    for(int j = 0; j < n; ++j) {
                        if(Flag[i][j] == 0) {
                            Ans[i] = j;
                        }
                    }
                    if(i + 1 < m) {
                        if(Flag[i + 1][Ans[i]] == 0) {
                            Flag[i + 1][Ans[i]] = -1;
                        }
                    }
                }
                printf("%s:%s", Name[Ans[i]], strstr(Msg[i], ":") + 1);
            }
        }
    }
    return 0;
}

Related Reading: Explore our comprehensive guide on Google Trends & technology research strategies.

Codeforces Round #390 (Div. 2), problem: (C) Vladik and Chat Solution in C/C++

#include <stdio.h>
#include <string.h>

int main()
{
    int t;
    scanf("%d", &t);
    while(t--) {
        int n, m;
        scanf("%d", &n);
        char Name[100][11];
        for(int i = 0; i < n; ++i) {
            scanf("%s", Name[i]);
        }
        scanf("%d\n", &m);
        char Msg[100][132];
        char Flag[100][100];
        int Cnt[100];
        int Ans[100];
        memset(Ans, -1, sizeof(Ans));
        memset(Cnt, 0, sizeof(Cnt));
        memset(Flag, 0, sizeof(Flag));
        for(int i = 0; i < m; ++i) {
            fgets(Msg[i], 131, stdin);
            if(Msg[i][0] == '?') {
                Cnt[i] = n;
                int start = 1;
                int end;
                while(Msg[i][start] != '\0') {
                    if((Msg[i][start] >= 'a' && Msg[i][start] <= 'z') || (Msg[i][start] >= 'A' && Msg[i][start] <= 'Z') || (Msg[i][start] >= '0' && Msg[i][start] <= '9')) {
                        int end = start;
                        while(Msg[i][end] != '\0') {
                            if((Msg[i][end] >= 'a' && Msg[i][end] <= 'z') || (Msg[i][end] >= 'A' && Msg[i][end] <= 'Z') || (Msg[i][end] >= '0' && Msg[i][end] <= '9')) {
                                ++end;
                            } else {
                                break;
                            }
                        }
                        char subStr[101];
                        int len = 0;
                        for(int j = start; j < end; ++j) {
                            subStr[len++] = Msg[i][j];
                        }
                        subStr[len] = '\0';
                        for(int j = 0; j < n; ++j) {
                            if(strcmp(subStr, Name[j]) == 0) {
                                if(Flag[i][j] == 0) {
                                    Flag[i][j] = -1;
                                    --Cnt[i];
                                    break;
                                }
                            }
                        }
                        start = end;
                    } else {
                        ++start;
                    }
                }
                if(Cnt[i] == 1) {
                    for(int j = 0; j < n; ++j) {
                        if(Flag[i][j] == 0) {
                            Ans[i] = j;
                            break;
                        }
                    }
                }
            } else {
                memset(Flag[i], -1, 100 * sizeof(char));
                char curName[11];
                int len = 0;
                for(int j = 0; Msg[i][j] != ':'; ++j) {
                    curName[len++] = Msg[i][j];
                }
                curName[len] = '\0';
                Cnt[i] = 1;
                for(int j = 0; j < n; ++j) {
                    if(strcmp(curName, Name[j]) == 0) {
                        Flag[i][j] = 0;
                        Ans[i] = j;
                        break;
                    }
                }
            }
        }
        for(int i = 0; i < m; ++i) {
            if(Cnt[i] == 1) {
                int cur = i;
                while(cur - 1 >= 0 && Flag[cur - 1][Ans[cur]] == 0) {
                    Flag[cur - 1][Ans[cur]] = -1;
                    if(--Cnt[cur - 1] != 1) {
                        break;
                    }
                    for(int j = 0; j < n; ++j) {
                        if(Flag[cur - 1][j] == 0) {
                            Ans[cur - 1] = j;
                            break;
                        }
                    }
                    --cur;
                }
                cur = i;
                while(cur + 1 < m && Flag[cur + 1][Ans[cur]] == 0) {
                    Flag[cur + 1][Ans[cur]] = -1;
                    if(--Cnt[cur + 1] != 1) {
                        break;
                    }
                    for(int j = 0; j < n; ++j) {
                        if(Flag[cur + 1][j] == 0) {
                            Ans[cur + 1] = j;
                            break;
                        }
                    }
                    ++cur;
                }
            }
        }
        char Impossible = 0;
        for(int i = 0; i < m; ++i) {
            if(Cnt[i] == 0) {
                Impossible = 1;
            }
        }
        if(Impossible) {
            puts("Impossible");
        } else {
            for(int i = 0; i < m; ++i) {
                if(Cnt[i] != 1) {
                    for(int j = 0; j < n; ++j) {
                        if(Flag[i][j] == 0) {
                            Ans[i] = j;
                        }
                    }
                    if(i + 1 < m) {
                        if(Flag[i + 1][Ans[i]] == 0) {
                            Flag[i + 1][Ans[i]] = -1;
                        }
                    }
                }
                printf("%s:%s", Name[Ans[i]], strstr(Msg[i], ":") + 1);
            }
        }
    }
    return 0;
}

Related Reading: Explore our comprehensive guide on Google Trends & technology research strategies.

More from author

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Related posts

Advertismentspot_img

Latest posts

I Tried Phone Stand For Desk For 30 Days Here Is the Honest Truth

📢 As an Amazon Associate, I earn from qualifying purchases. If you had told me six months ago that a single purchase would change how...

Stop Buying Cheap Alternatives This Wireless Earbuds Accessories Is the Real Deal

📢 As an Amazon Associate, I earn from qualifying purchases. I stumbled across this while desperately trying to fix a recurring problem in my routine....

Where the OKC Thunder Rank Among the Top 10 NBA Teams Entering August

As the offseason progresses into August, the NBA landscape continues to undergo dramatic transformations. Major headline-grabbing moves, including LeBron James making his historic transition...

Want to stay up to date with the latest news?

We would love to hear from you! Please fill in your details and we will stay in touch. It's that simple!