답안 #667339

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
667339 2022-12-01T07:16:02 Z marvinthang Type Printer (IOI08_printer) C++17
0 / 100
39 ms 36296 KB
#include <bits/stdc++.h>

using namespace std;

#define           REP(i, n)  for (int i = 0, _n = (n); i < _n; ++i)
#define          FORE(i, v)  for (__typeof((v).begin()) i = (v).begin(); i != (v).end(); ++i)
#define          file(name)  if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }

struct Node {
	Node *child[26];
	int terminal;
	Node() {
		REP(i, 26) child[i] = nullptr;
		terminal = 0;
	}
};

int N;
string maxS;
Node *root = new Node();

void addString(const string &s) {
	Node *p = root;
	FORE(it, s) {
		if (p->child[*it - 'a'] == nullptr) p->child[*it - 'a'] = new Node();
		p = p->child[*it - 'a'];
	}
	p->terminal++;
}

void init(void) {
	cin >> N;
	int sum = 0;
	REP(i, N) {
		string s; cin >> s;
		sum += s.size();
		if (maxS.size() < s.size()) maxS = s;
		addString(s);
	}
}

string res;

void dfs(Node *p, int depth, bool type) {
	REP(i, p->terminal) res += 'P';
	if (depth == maxS.size()) return;
	if (type) {
		REP(i, 26) if (p->child[i] != nullptr && i != maxS[depth] - 'a') {
			res += char(i + 'a');
			dfs(p->child[i], depth + 1, 0);
			res += '-';
		}
		res += maxS[depth];
		dfs(p->child[maxS[depth] - 'a'], depth + 1, 1);
	} else {
		REP(i, 26) if (p->child[i] != nullptr) {
			res += char(i + 'a');
			dfs(p->child[i], depth + 1, 0);
			res += '-';
		}
	}
}

void process(void) {
	dfs(root, 0, 1);
	cout << res.size() << '\n' << res;
}

int main(void) {
	ios_base::sync_with_stdio(false); cin.tie(nullptr); // cout.tie(nullptr);
	file("type-printer");
	init();
	process();
	return (0^0);
}

Compilation message

printer.cpp: In function 'void dfs(Node*, int, bool)':
printer.cpp:46:12: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   46 |  if (depth == maxS.size()) return;
      |      ~~~~~~^~~~~~~~~~~~~~
printer.cpp: In function 'int main()':
printer.cpp:7:67: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
    7 | #define          file(name)  if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
      |                                                            ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
printer.cpp:71:2: note: in expansion of macro 'file'
   71 |  file("type-printer");
      |  ^~~~
printer.cpp:7:100: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
    7 | #define          file(name)  if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
      |                                                                                             ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
printer.cpp:71:2: note: in expansion of macro 'file'
   71 |  file("type-printer");
      |  ^~~~
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 340 KB Line "tptttykduyvxjbzhqupP" doesn't correspond to pattern "[a-z\-P]{1}"
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 320 KB Line "eejzatjmnqxctnP--------------h...-yerxP----labfaryosskugbkiuffdP" doesn't correspond to pattern "[a-z\-P]{1}"
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 340 KB Line "hjxgqkP------iupqiqP------rhpq...------------wPfxlmwfirlgbdevjdP" doesn't correspond to pattern "[a-z\-P]{1}"
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 340 KB Line "bhvdxrpthaP----------edczvdgoy...--------------xomsgennpdlurnmvP" doesn't correspond to pattern "[a-z\-P]{1}"
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 340 KB Line "abbblauqkfdP----------edP--svh...----ooP--ulwzP----eynorwrbizaiP" doesn't correspond to pattern "[a-z\-P]{1}"
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 2 ms 1748 KB Line "aPaisxlhagqbjwbP-------------b...----------zP-cclviwgdudcybahuwP" doesn't correspond to pattern "[a-z\-P]{1}"
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 5 ms 5828 KB Line "aPacaoP---izjpfrpvhbP---------...------silP---zuknicjtukmwmlddzP" doesn't correspond to pattern "[a-z\-P]{1}"
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 13 ms 14668 KB Line "aPadmeP---ejuixzggakP---------...----vP-wshP---gPkwzakqubhstcdqP" doesn't correspond to pattern "[a-z\-P]{1}"
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 39 ms 36296 KB Line "aPaadfP--iznyxP-----nqjP----bo...------------nhbvobjbxuwlhzwchfP" doesn't correspond to pattern "[a-z\-P]{1}"
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 33 ms 28416 KB Line "aPaPbakyloxqrjP----------chfaP...vpiP--------jbfbP---jtearnhdjeP" doesn't correspond to pattern "[a-z\-P]{1}"
2 Halted 0 ms 0 KB -