Saturday, June 22, 2024

# 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;
}```

## Celebrating Black History Month:...

As February unfolds, so does the annual celebration of...

## The Path to Self-Mastery:...

Embarking on a journey of self-mastery and breaking free...

## Wizards of Waverly Place...

In a spellbinding announcement, Disney has officially revealed that...

## Jim Irsay’s Reported ‘Suspected...

In a shocking turn of events last month, Jim...

Coachella Valley Music and Arts Festival, one of the...

## 2024 Taiwan Election: Pivotal...

As Taiwan gears up for its 2024 presidential election,...

### Celebrating Black History Month: Past, Future

As February unfolds, so does the annual celebration of Black History Month, a time to reflect on the profound contributions, resilience, and rich cultural...

### The Path to Self-Mastery: Lessons from Book ‘The Mountain Is You’

Embarking on a journey of self-mastery and breaking free from self-sabotage is a transformative process that requires dedication and conscious effort. Brianna Wiest's insightful...

### Wizards of Waverly Place Cast Reunites for Enchanting Revival

In a spellbinding announcement, Disney has officially revealed that the beloved fantasy series "Wizards of Waverly Place" is set for a magical comeback, featuring...

### Jim Irsay’s Reported ‘Suspected Overdose’: A Closer Look

In a shocking turn of events last month, Jim Irsay, the owner of the Indianapolis Colts, was reportedly found unresponsive at his home in...

### Coachella 2024: Iconic headliners, unforgettable musical experience!

Coachella Valley Music and Arts Festival, one of the most iconic and eagerly anticipated music festivals globally, has just dropped its highly anticipated lineup...

### 2024 Taiwan Election: Pivotal Moment in Political Landscape

As Taiwan gears up for its 2024 presidential election, the political landscape is buzzing with anticipation and fervor. With the island nation situated at...

### Michael Strahan’s Daughter’s Medulloblastoma Diagnosis

In a recent and heartbreaking revelation, Michael Strahan, former NFL star and television personality, shared the devastating news of his daughter Isabella's diagnosis with...

### Michigan vs. Washington: The 2024 National Championship Clash

In a highly anticipated matchup, the 2024 National Championship will witness a clash of football titans as the Michigan Wolverines square off against the...

### Jason Kelce: Unmasking the Unconventional NFL Icon

In the world of professional football, where conformity often takes center stage, one player stands out as a beacon of individuality, both on and...