1 条题解

  • 0
    @ 2026-1-12 22:46:29

    #include <bits/stdc++.h>
    #define fail "impossible\n"
    using std::cin;
    using std::cout;
    using std::string;
    
    const int N = 4054;
    
    int n, n1, n2, nw;
    string s[N], universe;
    string Q1[N], Q2[N], Qw[N];
    
    bool is_subsequence(const string &s, const string &t) {
    	if (s.size() > t.size()) return false;
    	size_t pos = 0;
    	for (char ch : s) if ((pos = t.find(ch, pos))++ == string::npos) return false;
    	return true;
    }
    
    inline void push1(string &s) {
    	for (int i = 0; i < nw; ++i) Q2[n2++].swap(Qw[i]);
    	Q1[n1++].swap(s), nw = 0;
    }
    
    inline void push2(string &s) {
    	for (int i = 0; i < nw; ++i) Q1[n1++].swap(Qw[i]);
    	Q2[n2++].swap(s), nw = 0;
    }
    
    int main() {
    	int i; bool ok1, ok2;
    	std::ios::sync_with_stdio(false), cin.tie(NULL);
    	cin >> n >> universe;
    	for (i = 0; i < n; ++i)
    		if (cin >> s[i], !is_subsequence(s[i], universe)) return cout << fail, 0;
    	std::sort(s, s + n, [] (const string &s, const string &t) {return s.size() < t.size();});
    	for (i = 0; i < n; ++i) {
    		ok1 = !n1 || is_subsequence(Q1[n1 - 1], s[i]);
    		ok2 = !n2 || is_subsequence(Q2[n2 - 1], s[i]);
    		switch (ok1 | ok2 << 1) {
    			case 0: return cout << fail, 0;
    			case 1: push1(s[i]); break;
    			case 2: push2(s[i]); break;
    			case 3:
    				if (!nw || is_subsequence(Qw[nw - 1], s[i])) Qw[nw++].swap(s[i]);
    				else push1(s[i]);
    				break;
    		}
    	}
    	push1(s[n]), cout << --n1 << ' ' << n2 << '\n';
    	for (i = 0; i < n1; ++i) cout << Q1[i] << '\n';
    	for (i = 0; i < n2; ++i) cout << Q2[i] << '\n';
    	return 0;
    }
    
    
    • 1

    [ICPC 2015 WF] Evolution in Parallel(wuspj)

    信息

    ID
    5775
    时间
    2000ms
    内存
    1028MiB
    难度
    10
    标签
    递交数
    1
    已通过
    0
    上传者