CSES - Datatähti 2019 alku - Results
Submission details
Task:Leimasin
Sender:Yytsi
Submission time:2018-10-03 14:01:24 +0300
Language:C++
Status:READY
Result:29
Feedback
groupverdictscore
#1ACCEPTED29
#20
#30
Test results
testverdicttimegroup
#1ACCEPTED0.02 s1details
#2ACCEPTED0.01 s1details
#3ACCEPTED0.02 s1details
#4ACCEPTED0.01 s1details
#5ACCEPTED0.03 s1details
#6ACCEPTED0.01 s1details
#7ACCEPTED0.01 s1details
#8ACCEPTED0.02 s1details
#9ACCEPTED0.02 s1details
#10ACCEPTED0.01 s1details
#11ACCEPTED0.04 s1details
#12ACCEPTED0.02 s1details
#13ACCEPTED0.01 s1details
#14ACCEPTED0.02 s1details
#15ACCEPTED0.01 s2details
#16ACCEPTED0.02 s2details
#17ACCEPTED0.01 s2details
#18ACCEPTED0.01 s2details
#19ACCEPTED0.02 s2details
#20ACCEPTED0.03 s2details
#210.01 s2details
#22ACCEPTED0.02 s2details
#23ACCEPTED0.02 s2details
#24ACCEPTED0.02 s2details
#25ACCEPTED0.02 s2details
#26ACCEPTED0.02 s2details
#27ACCEPTED0.02 s2details
#28ACCEPTED0.02 s2details
#29ACCEPTED0.03 s3details
#30ACCEPTED0.02 s3details
#310.01 s3details
#32ACCEPTED0.03 s3details
#33ACCEPTED0.01 s3details
#34ACCEPTED0.02 s3details
#350.01 s3details
#360.02 s3details
#37ACCEPTED0.02 s3details
#38ACCEPTED0.02 s3details
#39ACCEPTED0.01 s3details
#40ACCEPTED0.03 s3details
#41ACCEPTED0.01 s3details
#42ACCEPTED0.02 s3details

Compiler report

input/code.cpp: In function 'std::vector<int> f(int, std::__cxx11::string)':
input/code.cpp:28:9: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  if (cnt==x.size()) return {};
      ~~~^~~~~~~~~~
input/code.cpp: In function 'void hsh(std::__cxx11::string)':
input/code.cpp:3:37: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
 #define FOR(i, a, b) for (int i=a; i<(b); i++)
                                     ^
input/code.cpp:94:2: note: in expansion of macro 'FOR'
  FOR(i,1,x.size()) {
  ^~~

Code

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i=a; i<(b); i++)
#define IO ios_base::sync_with_stdio(0); cin.tie(0)
#define ff first
#define ss second
#define pb push_back
#define INF 2147483647
#define LINF (1LL<<61LL)
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

#define N 1003

string s, L;
int n, m;

void fail() {
	cout<<"-1\n"; exit(0);
}

vector<int> f(int offset, string x) {
	if (x.size()==0) return {};
	int cnt = 0;
	for(char c:x) if(c=='?')cnt++;
	if (cnt==x.size()) return {};
	
	// ...??
	// or
	// ??...
	
	int sz = x.size();
	int idx = -1;
	FOR(i,0,sz-m+1) {
		if (i==0 && x[0]=='?') continue;
		bool found = true;
		FOR(j,0,m) {
			if (x[i+j] == '?') continue;
			if (x[i+j] != L[j]) {found=false; break;}
		}
		if (found) {
			idx = i;
			break;
		}
	}
	
	if (idx == -1) fail();
	
	FOR(j,0,m) x[idx+j] = '?';
	//cout<<x<<" "<<sz<<" offset: "<<offset<<" idx: "<<idx<<"\n";
	
	
	// Go left
	vector<int> left = f(offset+0, x.substr(0,idx+m));
	vector<int> right = f(offset+idx, x.substr(idx,string::npos));
	vector<int> res;
	for (int v:left) res.pb(v);
	for (int v:right) res.pb(v);
	res.pb(idx+offset);
	return res;
}

#define N 1003
ll A = 911382323, B = 972663749;
ll pA[N];
ll H[N], HASH[N];
ll pre[N], suff[N];
//vector<int> pres, sufs, full;
vector<int> ch;

struct Node {
	vector<int> pres, sufs, full;
} MOVE[N];

// 0-indexed
ll rq(int a, int b) {
	if (a == 0) return H[b];
	ll h = (H[b]-H[a-1]*pA[b-a+1])%B;
	if (h<0) h+=B;
	return h;
}

ll ran(int a, int b) {
	if (a==0) return HASH[b];
	ll h = (HASH[b]-HASH[a-1]*pA[b-a+1])%B;
	if (h<0) h+=B;
	return h;
}

void hsh(string x) {
	HASH[0] = x[0];
	FOR(i,1,x.size()) {
		HASH[i] = (HASH[i-1]*A+x[i])%B;
	}
	FOR(l,0,m) {
		ll a = ran(0, l);
		ll b = ran(m-1-l,m-1);
		if (a < 0) a += B;
		if (b < 0) b += B;
		pre[l+1] = a;
		suff[l+1] = b;
	}
}

ll pure(const string& x) {
	ll h = 0;
	for (char c:x) h=(h*A+(ll)c)%B;
	return h;
}

// {(a,b), moves to make this segment}
set<pair<pii, int>> vr;
bool proc[N]; // processed
int pr = 0;

string w = "";
void paint(int p) {
	FOR(j,0,m) w[j+p] = L[j]; 
}

void solved() {
	/*
	int sz=pres.size()+full.size()+sufs.size();
	reverse(pres.begin(), pres.end());
	reverse(sufs.begin(), sufs.end());
	for (int pr : pres) paint(pr);
	for (int sf : sufs) paint(sf);
	for (int f : full) paint(f);
	
	cout<<sz<<"\n";
	for (int pr : pres) cout<<pr+1<<" ";
	for (int sf : sufs) cout<<sf+1<<" ";
	for (int f : full) cout<<f+1<<" ";*/
	vector<int> seq;
	for (int idx : ch) {
		// check out MOVE[idx]
		Node p = MOVE[idx];
		vector<int> pres = p.pres;
		vector<int> sufs = p.sufs;
		vector<int> full = p.full;
		sort(pres.begin(), pres.end());
		sort(sufs.begin(), sufs.end());
		reverse(sufs.begin(), sufs.end());
		for (int v : pres) seq.pb(v);
		for (int v : sufs) seq.pb(v);
		for (int v : full) seq.pb(v);
	}
	
	FOR(j,0,n) w[j]='?';
	for (int v:seq) paint(v);
	if (w != s) {
		// let's double check...
		vector<int> conf = f(0, s);
		FOR(j,0,n) w[j]='?';
		for(int v:conf)paint(v);
		if (w == s) {
			cout<<conf.size()<<"\n";
			for(int v:conf)cout<<v+1<<" ";
			exit(0);
		} else {
			cout<<"-1\n"; exit(0);
		}
	}
	cout<<seq.size()<<"\n";
	for(int v:seq)cout<<v+1<<" ";
	exit(0);
}

void addProc(int x) {
	if (!proc[x]) pr++;
	proc[x] = true;
	if (pr == n) solved();
}

// inclusive
void process(int a, int b) {
	FOR(j,a,b+1) addProc(j);
}

int main() {
	IO;
	cin>>s; cin>>L;
	n = s.size();
	m = L.size();
	
	pA[0] = 1;
	H[0] = s[0];
	FOR(i,1,n+1) {
		H[i] = (H[i-1]*A+s[i])%B;
		pA[i] = pA[i-1]*A%B;
	}
	
	FOR(j,0,n) w+="?";
	
	hsh(L);
	ll label = pure(L);
	
	// Find all labels
	FOR(i,0,n-m+1) {
		if (proc[i]) continue;
		// Match for label
		if (rq(i,i+m-1) == label) {
			MOVE[i].full.pb(i);
			ch.pb(i);
			vr.insert({{i,i+m-1}, i});
			process(i,i+m-1);
		}
	}
	
	
	while (vr.size()) {
		//makeCompact();
		// Try to spread a segment
		bool ext = false;
		auto seg = *vr.begin();
		int a = seg.ff.ff, b = seg.ff.ss;
		int midx = seg.ss;
		
		// Check for any prefix
		for (int l=1; l<=m; l++) {
			int lp = a-l;
			if (lp < 0) continue;
			if (rq(lp,a-1) == pre[l]) {
				//cout<<"found prefix at "<<lp+1<<"\n";
				vr.erase(vr.begin());
				vr.insert({{lp, b}, midx});
				MOVE[midx].pres.pb(lp);
				//pres.pb(lp);
				process(lp, a-1);
				ext = true;
				break;
			}
		}
		
		if (ext) continue;
		
		// Check for any suffix, as no prefix was found
		for (int l=1; l<=m; l++) {
			int rp = b+l;
			if (rp >= n) continue;
			if (rq(b+1,rp) == suff[l]) {
				vr.erase(vr.begin());
				vr.insert({{a,rp}, midx});
				MOVE[midx].sufs.pb(rp-m+1);
				process(b+1, rp);
				ext = true;
			}
		}
		
		if (ext) continue;
		
		vr.erase(vr.begin());
	}
	
	cout<<"-1\n";
}






Test details

Test 1

Group: 1

Verdict: ACCEPTED

input
BBBBBBBBBB
B

correct output
10
10 9 8 7 6 5 4 3 2 1 

user output
10
1 2 3 4 5 6 7 8 9 10 

Test 2

Group: 1

Verdict: ACCEPTED

input
AABBABABAB
AB

correct output
6
1 9 7 5 3 2 

user output
6
1 3 2 5 7 9 

Test 3

Group: 1

Verdict: ACCEPTED

input
AABAAABAAA
AABAA

correct output
4
6 5 2 1 

user output
4
6 5 2 1 

Test 4

Group: 1

Verdict: ACCEPTED

input
BAAAAAABBB
BAAAAAABB

correct output
2
2 1 

user output
2
2 1 

Test 5

Group: 1

Verdict: ACCEPTED

input
AAABBABBAA
AAABBABBAA

correct output
1

user output
1

Test 6

Group: 1

Verdict: ACCEPTED

input
GGGGGGGGGG
G

correct output
10
10 9 8 7 6 5 4 3 2 1 

user output
10
1 2 3 4 5 6 7 8 9 10 

Test 7

Group: 1

Verdict: ACCEPTED

input
QUUQUUQUQU
QU

correct output
6
9 7 5 4 2 1 

user output
7
5 4 2 1 4 7 9 

Test 8

Group: 1

Verdict: ACCEPTED

input
DWXDWDWXHJ
DWXHJ

correct output
3
1 4 6 

user output
3
1 4 6 

Test 9

Group: 1

Verdict: ACCEPTED

input
FSOCRDGQBB
FSOCRDGQB

correct output
2
2 1 

user output
2
2 1 

Test 10

Group: 1

Verdict: ACCEPTED

input
OETMIMPUPD
OETMIMPUPD

correct output
1

user output
1

Test 11

Group: 1

Verdict: ACCEPTED

input
DOWEUOWUEU
DOWEU

correct output
-1

user output
-1

Test 12

Group: 1

Verdict: ACCEPTED

input
JQZYVSIWTE
JQZVYSIWTE

correct output
-1

user output
-1

Test 13

Group: 1

Verdict: ACCEPTED

input
ABABABABA
ABA

correct output
4
7 5 3 1 

user output
5
7 5 3 1 5 

Test 14

Group: 1

Verdict: ACCEPTED

input
AAAAAAAAAA
AAAAAAAAAB

correct output
-1

user output
-1

Test 15

Group: 2

Verdict: ACCEPTED

input
BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB...

correct output
100
100 99 98 97 96 95 94 93 92 91...

user output
100
1 2 3 4 5 6 7 8 9 10 11 12 13 ...

Test 16

Group: 2

Verdict: ACCEPTED

input
BABABAAAAAAAAAAAAAAAAAABABAAAA...

correct output
36
87 43 24 1 91 79 69 68 67 66 6...

user output
46
1 14 13 12 11 10 9 8 7 6 5 4 3...

Test 17

Group: 2

Verdict: ACCEPTED

input
ABABAAAAABABBBBAAAABBBBAABBBBB...

correct output
22
51 50 43 41 31 28 26 24 21 20 ...

user output
6
51 50 43 41 3 1 

Test 18

Group: 2

Verdict: ACCEPTED

input
AAABABAAAABBBBBABABBAABBABABBA...

correct output
2
1 2 

user output
2
1 2 

Test 19

Group: 2

Verdict: ACCEPTED

input
AABABBBBBBAABBABABBBBBBAABBAAA...

correct output
1

user output
1

Test 20

Group: 2

Verdict: ACCEPTED

input
SSSSSSSSSSSSSSSSSSSSSSSSSSSSSS...

correct output
100
100 99 98 97 96 95 94 93 92 91...

user output
100
1 2 3 4 5 6 7 8 9 10 11 12 13 ...

Test 21

Group: 2

Verdict:

input
NNNININIMNIMKLMXCNIMKLMXCDEIMK...

correct output
18
1 2 3 74 5 79 58 7 84 64 37 10...

user output
-1

Test 22

Group: 2

Verdict: ACCEPTED

input
VYQFNHMVTKOEYCXWINLKLHVFMEPQEU...

correct output
3
51 2 1 

user output
2
1 51 

Test 23

Group: 2

Verdict: ACCEPTED

input
IISNROLHLOJIWPTVFHFLUQRIROVLYP...

correct output
2
1 2 

user output
2
1 2 

Test 24

Group: 2

Verdict: ACCEPTED

input
WPMEMERJXXADLKONUZPUUFTPSXDHIV...

correct output
1

user output
1

Test 25

Group: 2

Verdict: ACCEPTED

input
LNSBGZAWFJZAWFJWFJLNSBLNSBGZAL...

correct output
-1

user output
-1

Test 26

Group: 2

Verdict: ACCEPTED

input
IPIPYFUMRIPYFUMRLPIIIPYFIPYFUM...

correct output
-1

user output
-1

Test 27

Group: 2

Verdict: ACCEPTED

input
ABABABABABABABABABABABABABABAB...

correct output
49
97 95 93 91 89 87 85 83 81 79 ...

user output
72
95 93 91 89 87 85 83 81 79 77 ...

Test 28

Group: 2

Verdict: ACCEPTED

input
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA...

correct output
-1

user output
-1

Test 29

Group: 3

Verdict: ACCEPTED

input
BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB...

correct output
1000
1000 999 998 997 996 995 994 9...

user output
1000
1 2 3 4 5 6 7 8 9 10 11 12 13 ...

Test 30

Group: 3

Verdict: ACCEPTED

input
BBBBBBBBAABBBBBBBBAABBBBBBBAAB...

correct output
218
1 626 607 519 415 5 975 957 92...

user output
418
1 2 3 4 5 11 12 13 14 15 21 22...

Test 31

Group: 3

Verdict:

input
AABBBABAABABAAABBAAAAAAABBBAAB...

correct output
55
569 639 403 761 663 437 172 90...

user output
-1

Test 32

Group: 3

Verdict: ACCEPTED

input
ABBAAABAAABAAAAABBABABBABBABBB...

correct output
2
2 1 

user output
2
2 1 

Test 33

Group: 3

Verdict: ACCEPTED

input
BAAABBABBBAAAABAAAABBBBABAABAA...

correct output
1

user output
1

Test 34

Group: 3

Verdict: ACCEPTED

input
UUUUUUUUUUUUUUUUUUUUUUUUUUUUUU...

correct output
1000
1000 999 998 997 996 995 994 9...

user output
1000
1 2 3 4 5 6 7 8 9 10 11 12 13 ...

Test 35

Group: 3

Verdict:

input
KSBMRKKSBMRZXBDKSKSBMRZXBDAMRZ...

correct output
178
723 731 1 935 857 820 760 735 ...

user output
-1

Test 36

Group: 3

Verdict:

input
ILYLILYLVJILYLVJZCCQDLFRLSXZDM...

correct output
21
671 54 747 504 113 1 856 764 5...

user output
-1

Test 37

Group: 3

Verdict: ACCEPTED

input
ZZJZNKHDLJBPXIAZNJIIGBEEJFSDAF...

correct output
2
1 2 

user output
2
1 2 

Test 38

Group: 3

Verdict: ACCEPTED

input
FIMWTOLSRKOWYDPCOFUJZMXJEJFKSU...

correct output
1

user output
1

Test 39

Group: 3

Verdict: ACCEPTED

input
AIVHCGUMKSTIYBRNPONXHRFVBKPYHX...

correct output
-1

user output
-1

Test 40

Group: 3

Verdict: ACCEPTED

input
QPMSLIDCLFLBEXGVVQQNSVKJYXGETC...

correct output
-1

user output
-1

Test 41

Group: 3

Verdict: ACCEPTED

input
ABABABABABABABABABABABABABABAB...

correct output
499
997 995 993 991 989 987 985 98...

user output
747
995 993 991 989 987 985 983 98...

Test 42

Group: 3

Verdict: ACCEPTED

input
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA...

correct output
-1

user output
-1