답안 #28293

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
28293 2017-07-16T04:27:25 Z 볼빨간 승관이(#1152, sys7961, deneb2016, hyorothy) Play Onwards (FXCUP2_onward) C++11
1 / 1
23 ms 2296 KB
#include<bits/stdc++.h>

int n, k;
char str[210][25];
long long B = 255;
bool connect(int a, int b) {
	std::set<long long> set;
	for (int i = 0; str[a][i + k - 1] != '\0'; i++) {
		long long h = 0;
		for (int j = 0; j < k; j++) {
			h *= B;
			h += str[a][i + j];
		}
		set.insert(h);
	}
	for (int i = 0; str[b][i + k - 1] != '\0'; i++) {
		long long h = 0;
		for (int j = 0; j < k; j++) {
			h *= B;
			h += str[b][i + j];
		}
		if (set.count(h))return true;
	}
	return false;

}
using std::vector;
vector<int> G[210];
int color[210];
bool dfs(int idx) {
	for(int i=0;i<G[idx].size();i++) {
		int to = G[idx][i];
		if (color[to] == 0) {
			color[to] = -color[idx];
			if (!dfs(to)) {
				return false;
			}
		}
		else if (color[to] == color[idx]) {
			return false;
		}
	}
	return true;
}
int main() {
	scanf("%d%d", &n, &k);
	for (int i = 0; i < n; i++) {
		scanf("%s", str[i]);
	}
	for (int i = 0; i < n; i++) {
		for (int j = i + 1; j < n; j++) {
			if (connect(i, j)) {
				G[i].push_back(j);
				G[j].push_back(i);
			}
		}
	}
	int p = 1;
	for (int i = 0; i < n; i++) {
		if (color[i] == 0) {
			color[i] = p;
			p = -p;
			if (!dfs(i)) {
				printf("No");
				return 0;
			}
		}
	}
	printf("Yes\n");
	for (int i = 0; i < n; i++) {
		if (color[i] == -1) {
			printf("2\n");
		}
		else {
			printf("1\n");
		}
	}

}

Compilation message

onward.cpp: In function 'bool dfs(int)':
onward.cpp:31:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0;i<G[idx].size();i++) {
               ^
onward.cpp: In function 'int main()':
onward.cpp:46:23: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d%d", &n, &k);
                       ^
onward.cpp:48:22: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%s", str[i]);
                      ^
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 2032 KB Output is correct
2 Correct 0 ms 2032 KB Output is correct
3 Correct 0 ms 2032 KB Output is correct
4 Correct 0 ms 2032 KB Output is correct
5 Correct 0 ms 2296 KB Output is correct
6 Correct 9 ms 2296 KB Output is correct
7 Correct 23 ms 2032 KB Output is correct
8 Correct 13 ms 2032 KB Output is correct
9 Correct 13 ms 2032 KB Output is correct
10 Correct 16 ms 2032 KB Output is correct
11 Correct 3 ms 2032 KB Output is correct
12 Correct 13 ms 2032 KB Output is correct
13 Correct 9 ms 2032 KB Output is correct
14 Correct 6 ms 2032 KB Output is correct
15 Correct 0 ms 2032 KB Output is correct
16 Correct 0 ms 2032 KB Output is correct
17 Correct 0 ms 2032 KB Output is correct
18 Correct 0 ms 2032 KB Output is correct
19 Correct 13 ms 2032 KB Output is correct
20 Correct 9 ms 2032 KB Output is correct
21 Correct 9 ms 2032 KB Output is correct