## Codeforces Round #290 (Div. 2)

2019年02月19日 ⁄ 综合 ⁄ 共 6772字 ⁄ 字号 评论关闭
﻿﻿

### A. Fox And Snake Fox Ciel starts to learn programming. The first task is drawing a fox! However, that turns out to be too hard for a beginner, so she decides to draw a snake instead. A snake is a pattern on a n by m table. Denote c-th cell of r-th row as (r, c). The tail of the snake is located at (1, 1), then it's body extends to (1, m), then goes down 2 rows to (3, m), then goes left to (3, 1) and so on. Your task is to draw this snake for Fox Ciel: the empty cells should be represented as dot characters ('.') and the snake cells should be filled with number signs ('#'). Consider sample tests in order to understand the snake pattern. Input The only line contains two integers: n and m (3 ≤ n, m ≤ 50). n is an odd number. Output Output n lines. Each line should contain a string consisting of m characters. Do not output spaces. Sample test(s) input ```3 3 ``` output ```### ..# ### ``` input ```3 4 ``` output ```#### ...# #### ``` input ```5 3 ``` output ```### ..# ### #.. ### ``` input ```9 9 ``` output ```######### ........# ######### #........ ######### ........# ######### #........ #########```

简单打印图形。
```#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <math.h>
using namespace std;

int n, m;

int main()
{
while(~scanf("%d%d",&n,&m))
{
int cnt = 1;
for(int i = 1; i <= n; i ++)
{
if(i %2 == 1)
{
for(int j = 0; j < m; j ++)
{
printf("#");
}
}
else if(i % 2 == 0 && cnt % 2 == 1)
{
cnt ++;
for(int j = 1; j < m; j ++)
{
printf(".");
}
printf("#");
}
else if(i % 2 == 0 && cnt % 2 == 0)
{
cnt ++;
printf("#");
for(int j = 1; j < m; j ++)
{
printf(".");
}
}
printf("\n");
}
}
}```

### B. Fox And Two Dots

Fox Ciel is playing a mobile puzzle game called "Two Dots". The basic levels are played on a board of size n × m cells, like this: Each cell contains a dot that has some color. We will use different uppercase Latin characters to express different colors.

The key of this game is to find a cycle that contain dots of same color. Consider 4 blue dots on the picture forming a circle as an example. Formally, we call a sequence of dots d1, d2, ..., dk a cycle if
and only if it meets the following condition:

1. These k dots are different: if i ≠ j then di is
different from dj.
2. k is at least 4.
3. All dots belong to the same color.
4. For all 1 ≤ i ≤ k - 1di and di + 1 are
adjacent. Also, dk and d1 should
if they share an edge.

Determine if there exists a cycle on the field.

Input

The first line contains two integers n and m (2 ≤ n, m ≤ 50):
the number of rows and columns of the board.

Then n lines follow, each line contains a string consisting of m characters,
expressing colors of dots in each line. Each character is an uppercase Latin letter.

Output

Output "Yes" if there exists a cycle, and "No"
otherwise.

Sample test(s)
input
```3 4
AAAA
ABCA
AAAA
```
output
```Yes
```
input
```3 4
AAAA
ABCA
```
output
```No
```
input
```4 4
YYYR
BYBY
BBBY
BBBY
```
output
```Yes
```
input
```7 6
AAAAAB
ABBBAB
ABAAAB
ABABBB
ABAAAB
ABBBAB
AAAAAB
```
output
```Yes
```
input
```2 13
ABCDEFGHIJKLM
NOPQRSTUVWXYZ
```
output
```No
```
Note

In first sample test all 'A' form a cycle.

In second sample there is no such cycle.

The third sample is displayed on the picture above ('Y' = Yellow, 'B'
= Blue, 'R' = Red).

搜索，判断是否存在环，即相同字母构成的环。我发现，我的搜索还是很弱，递归方面的理解，感觉还是不透彻

```<span style="color:#222222;">#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <math.h>
#include <queue>
#include <vector>
#include <map>
using namespace std;

#define N 55

int dis = {
{-1, 0}, {0, 1}, {1, 0}, {0, -1}
};

bool flag = false;
char ss[N][N];
bool vis[N][N];

int n, m;

void dfs(int x, int y, int sx, int sy, char t)
{

if(x < 1 || x > n || y < 1 || y > m)
return ;
if(vis[x][y])
{
flag = true;
return;
}
vis[x][y] = true;

for(int i = 0; i < 4; i ++)
{
int xx = x + dis[i];
int yy = y + dis[i];
if(xx < 1 || xx > n || yy < 1 || yy > m)
continue;
if(ss[xx][yy] == t)
{
if(!(xx == sx && yy == sy))
dfs(xx, yy, x, y, t);
}
}
}

int main()
{
while(~scanf("%d%d",&n,&m))
{
memset(ss, '0', sizeof(ss));
getchar();
for(int i = 1; i <= n; i ++)
{
for(int j = 1; j <= m; j ++)
{
scanf("%c",&ss[i][j]);
}
getchar();
}

flag = false;

for(int i = 1; i <= n; i ++)
{
for(int j = 1; j <= m; j ++)
{
memset(vis, false, sizeof(vis));
dfs(i, j, -1, -1, ss[i][j]);
if(flag)
break;
}
}
if(flag)
printf("Yes\n");
else
printf("No\n");

}
return 0;
}</span><span style="color:#33ff33;">
</span>```

#### C. Fox And Names

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Fox Ciel is going to publish a paper on FOCS (Foxes Operated Computer Systems, pronounce: "Fox"). She heard a rumor: the authors list on the paper is always sorted in the lexicographical order.

After checking some examples, she found out that sometimes it wasn't true. On some papers authors' names weren't sorted inlexicographical order in normal sense. But it was always true that after
some modification of the order of letters in alphabet, the order of authors becomes lexicographical!

She wants to know, if there exists an order of letters in Latin alphabet such that the names on the paper she is submitting are following in the lexicographical order. If so, you should find out
any such order.

Lexicographical order is defined in following way. When we compare s and t,
first we find the leftmost position with differing characters:si ≠ ti.
If there is no such position (i. e. s is a prefix of t or
vice versa) the shortest string is less. Otherwise, we compare characters siand ti according
to their order in alphabet.

Input

The first line contains an integer n (1 ≤ n ≤ 100):
number of names.

Each of the following n lines contain one string namei (1 ≤ |namei| ≤ 100),
the i-th name. Each name contains only lowercase Latin letters. All names are different.

Output

If there exists such order of letters that the given names are sorted lexicographically, output any such order as a permutation of characters 'a'–'z' (i. e. first output the first letter of the modified alphabet, then the second, and so on).

Otherwise output a single word "Impossible" (without quotes).

Sample test(s)
input
```3
rivest
shamir
```
output
```bcdefghijklmnopqrsatuvwxyz
```
input
```10
tourist
petr
wjmzbmr
yeputons
vepifanov
scottwu
oooooooooooooooo
subscriber
rowdark
tankengineer
```
output
```Impossible
```
input
```10
petr
egor
endagorion
feferivan
ilovetanyaromanova
kostka
dmitriyh
maratsnowbear
bredorjaguarturnik
cgyforever
```
output
```aghjlnopefikdmbcqrstuvwxyz
```
input
```7
car
care
careful
carefully
becarefuldontforgetsomething
otherwiseyouwillbehacked
goodluck
```
output
`acbdefhijklmnogpqrstuvwxyz`

```#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <math.h>
using namespace std;

#define N 110

int in[N];
int ans[N];
char tt[N][N];
bool mpt[N][N];
int cnt;

bool topo()
{
cnt = 0;
for(int i = 0; i < 26; i ++)
{
int k = 0;
while(in[k] != 0 && k < 26)
k ++;
if(k == 26)
return false;
ans[cnt ++] = k;
in[k] = -1;
for(int j = 0; j < 26; j ++)
{
if(mpt[k][j])
in[j] --;
}
}
if(cnt < 26)
return false;
else
return true;
}

int main()
{
int n;
while(~scanf("%d",&n))
{
memset(tt, '0', sizeof(tt));
memset(mpt, false, sizeof(mpt));
memset(in, 0, sizeof(in));
memset(ans, 0, sizeof(ans));

for(int i = 0; i < n; i ++)
{
scanf("%s",tt[i]);
}
bool flag = true;
for(int i = 0; i < n - 1; i ++)
{
int sta = 0;
int len1 = strlen(tt[i]);
int len2 = strlen(tt[i + 1]);
while(tt[i][sta] == tt[i + 1][sta] &&(sta < min(len1, len2)))
sta ++;
if(sta == min(len1, len2) && len1 > len2)  // 处理类似第十三组这样的数据的数据
{

flag = false;
continue;
}

if(!mpt[tt[i][sta] - 'a'][tt[i + 1][sta] - 'a'])  // 这步不可以少，不然 会重复计数
{
mpt[tt[i][sta] - 'a'][tt[i + 1][sta] - 'a'] = true;
in[tt[i + 1][sta] - 'a'] ++;
}
}
cnt = 0;
if(!flag)
{
printf("Impossible\n");
continue;;
}

flag = topo();
if(flag)
{
for(int i  = 0; i < cnt; i ++)
{
printf("%c",ans[i] + 'a');
}
}
else
printf("Impossible");
printf("\n");
}
}

/**************

INPUT
2
aa
a

OUTPUT
Impossible

*****/
```