답안 #22948

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
22948 2017-04-30T16:46:52 Z gs14004 Joyful KMP (KRIII5_JK) C++11
7 / 7
366 ms 85180 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long lint;
typedef unsigned long long kriii;
typedef long double llf;
typedef pair<int, int> pi;
const int mod = 1e9 + 7;
 
struct disj{
	int pa[1000005];
	void init(int n){
		for(int i=0; i<=n; i++) pa[i] = i;
	}
	int find(int x){
		return pa[x] = (pa[x] == x ? x : find(pa[x]));
	}
	bool uni(int p, int q){
		p = find(p);
		q = find(q);
		if(p == q) return 0;
		pa[q] = p; return 1;
	}
}disj;
 
vector<int> gph[1000005];
vector<pi> v;
char str[1000005];
char rchr[1000005];
int fail[1000005];
kriii cnt[1000005];
lint k;
 
int n, deg[1000005];
 
int main(){
	cin >> str >> k;
	n = strlen(str);
	disj.init(n);
	int p = 0;
	for(int i=1; str[i]; i++){
		while(p > 0 && str[i] != str[p]){
			v.push_back(pi(p, i));
			p = fail[p];
		}
		if(str[i] == str[p]){
		//	printf("uni %d %d\n", i, p);
			disj.uni(p, i);
			p++;
		}
		else{
			v.push_back(pi(p, i));
		}
		fail[i+1] = p;
	}
	for(auto &i : v){
		i.first = disj.find(i.first);
		i.second = disj.find(i.second);
		if(i.first > i.second) swap(i.first, i.second);
	}
	sort(v.begin(), v.end());
	v.resize(unique(v.begin(), v.end()) - v.begin());
	for(auto &i : v){
		deg[i.second]++;
		gph[i.second].push_back(i.first);
	}
	lint ans = 1;
	for(int i=0; i<n; i++){
		if(disj.find(i) == i){
			ans *= max(0, 26 - deg[i]);
			ans %= mod;
		}
	}
	cout << ans << endl;
	kriii tmp = 1;
	for(int i=n-1; i>=0; i--){
		if(disj.find(i) == i){
			cnt[i] = tmp;
			tmp = 0;
			for(int j=0; j<26-deg[i]; j++){
				tmp += cnt[i];
				if(tmp > k) tmp = k+1;
			}
		}
	}
	if(tmp < k){
		cout << "OVER";
		return 0;
	}
	else{
		k--;
		for(int i=0; i<n; i++){
			if(disj.find(i) == i){
				lint x = k / cnt[i];
				k -= x * cnt[i];
				int mark[26] = {};
				for(auto &j : gph[i]){
					mark[rchr[j] - 'a'] = 1;
				}
				lint prvx = x;
				for(int j=0; j<26; j++){
					if(!mark[j]) x--;
					if(x == -1){
						rchr[i] = j + 'a';
						break;
					}
				}
			}
		}
		for(int i=0; i<n; i++) putchar(rchr[disj.find(i)]);
	}
}

Compilation message

JK.cpp: In function 'int main()':
JK.cpp:81:12: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     if(tmp > k) tmp = k+1;
            ^
JK.cpp:85:9: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  if(tmp < k){
         ^
JK.cpp:99:10: warning: unused variable 'prvx' [-Wunused-variable]
     lint prvx = x;
          ^
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 46944 KB Output is correct
2 Correct 6 ms 46944 KB Output is correct
3 Correct 3 ms 46944 KB Output is correct
4 Correct 0 ms 46944 KB Output is correct
5 Correct 289 ms 85180 KB Output is correct
6 Correct 366 ms 85180 KB Output is correct
7 Correct 336 ms 85180 KB Output is correct
8 Correct 343 ms 85180 KB Output is correct
9 Correct 276 ms 85180 KB Output is correct
10 Correct 3 ms 46944 KB Output is correct
11 Correct 0 ms 46944 KB Output is correct
12 Correct 6 ms 46944 KB Output is correct
13 Correct 9 ms 47084 KB Output is correct
14 Correct 9 ms 47084 KB Output is correct
15 Correct 19 ms 48064 KB Output is correct
16 Correct 26 ms 49104 KB Output is correct
17 Correct 119 ms 56796 KB Output is correct
18 Correct 273 ms 66964 KB Output is correct
19 Correct 256 ms 67624 KB Output is correct
20 Correct 229 ms 67360 KB Output is correct
21 Correct 236 ms 66964 KB Output is correct
22 Correct 213 ms 66700 KB Output is correct
23 Correct 63 ms 46944 KB Output is correct
24 Correct 69 ms 46944 KB Output is correct
25 Correct 56 ms 46944 KB Output is correct
26 Correct 53 ms 46944 KB Output is correct
27 Correct 63 ms 46944 KB Output is correct
28 Correct 63 ms 46944 KB Output is correct
29 Correct 73 ms 46944 KB Output is correct
30 Correct 56 ms 46944 KB Output is correct
31 Correct 66 ms 46944 KB Output is correct
32 Correct 63 ms 46944 KB Output is correct
33 Correct 53 ms 46944 KB Output is correct
34 Correct 69 ms 46944 KB Output is correct
35 Correct 63 ms 46944 KB Output is correct
36 Correct 63 ms 46944 KB Output is correct
37 Correct 69 ms 46944 KB Output is correct
38 Correct 73 ms 46944 KB Output is correct
39 Correct 59 ms 46944 KB Output is correct
40 Correct 69 ms 46944 KB Output is correct
41 Correct 56 ms 46944 KB Output is correct
42 Correct 66 ms 46944 KB Output is correct
43 Correct 63 ms 46944 KB Output is correct
44 Correct 76 ms 46944 KB Output is correct
45 Correct 66 ms 46944 KB Output is correct
46 Correct 63 ms 46944 KB Output is correct
47 Correct 83 ms 46944 KB Output is correct
48 Correct 66 ms 46944 KB Output is correct
49 Correct 46 ms 46944 KB Output is correct
50 Correct 59 ms 46944 KB Output is correct