CSES - Datatähti 2018 loppu - Results
Submission details
Task:Merkkijono
Sender:Yytsi
Submission time:2018-01-18 16:37:38 +0200
Language:C++
Status:READY
Result:100
Feedback
groupverdictscore
#1ACCEPTED100
Test results
testverdicttime
#1ACCEPTED0.05 sdetails
#2ACCEPTED0.02 sdetails
#3ACCEPTED0.05 sdetails
#4ACCEPTED0.05 sdetails
#5ACCEPTED0.05 sdetails

Code

#include <iostream>
#include <string>
#include <vector>

using namespace std;

vector<int> lst;
vector<int> oper;
int k = 0;
#define N 26
bool popped = false;

void printLst() {
	for (int i = 0; i < (int)lst.size(); i++) {
		cout << lst[i] << " ";
	}
	cout<<"\n";
}

bool sorted() {
	for (int i = 0; i < (int)lst.size()-1; i++) {
		if (lst[i] > lst[i + 1]) return false;
	}
	return true;
}
int ab(int a, int b) {
	return (a<b)?(b-a):(a-b);
}

void sw() {
	int f = lst[1]; lst[1] = lst[0]; lst[0] = f;
	oper.push_back(0);
	k++;
	//cout << "Swap: ";
	//printLst();
}
void mov() {
	int last = lst[N - 1];
	lst.pop_back();
	lst.insert(lst.begin(), last);
	oper.push_back(1);
	k++;
	//cout << "Move: ";
	//printLst();
}
int findIndex(int x) {
	for (int i = 0; i < N; i++) {
		if (lst[i] == x) return i;
	}
	return -1;
}
void moveTo(int x, int to) {
	int loc = findIndex(x);
	int passBy = loc - to;
	while (lst[0] != x) mov();
	// x ...
	// moveTo(4, 1)
	// 1 [2 3] 4 5  [...] passBy
	// 1 4 2 3 5
	// now: 4 5 1 2 3
	// Let the bypassers through.
	for (int i = 0; i < passBy; i++) {
		mov(); sw();
	}
	while (lst[to] != x) mov();
}

int main()
{
	string s; cin >> s;
	for (int i = 0; i < (int)s.size(); i++) {
		char c = s[i];
		lst.push_back((int)c - 65);
		//cout<<((int)c-65)<<" ";
	}
	//cout<<"\n";
	
	for (int i = 0; i < N; i++) {
		if (sorted()) break;
		moveTo(i, i);
	}
	
	cout << k << "\n";
	for (int i = 0; i < (int)oper.size(); i++) {
		if (oper[i] == 0) cout << "SWAP\n";
		else cout << "MOVE\n";
	}
	
	
	return 0;
}

Test details

Test 1

Verdict: ACCEPTED

input
ABCDEFGHIJKLMNOPQRSTUVWXYZ

correct output
0

user output
0

Test 2

Verdict: ACCEPTED

input
ZYXWVUTSRQPONMLKJIHGFEDCBA

correct output
923
MOVE
MOVE
SWAP
MOVE
...

user output
975
MOVE
MOVE
SWAP
MOVE
...

Test 3

Verdict: ACCEPTED

input
RPJMFWBHYQOTXUAENLDGZISCVK

correct output
611
SWAP
MOVE
MOVE
SWAP
...

user output
795
MOVE
MOVE
MOVE
MOVE
...

Test 4

Verdict: ACCEPTED

input
GWJSPBHANMXYFLKIDORVUCEZQT

correct output
659
MOVE
SWAP
MOVE
SWAP
...

user output
715
MOVE
MOVE
MOVE
MOVE
...

Test 5

Verdict: ACCEPTED

input
BJYNFLKEIUCZMQHRAXOGWPSDTV

correct output
624
MOVE
SWAP
MOVE
SWAP
...

user output
756
MOVE
MOVE
MOVE
MOVE
...