The interesting question related to the sequence of four

(8) Given an English word, which eliminate repeated letters, only letters can be deleted. Can not exchange alphabetical order of the last letter of each of the original word appears only once, the result of evaluating the lexicographically smallest.

This is toj a problem. Baidu interview also asked the original question see http://acm.tju.edu.cn/toj/showp3257.html

I like this question, it is a trick of the algorithm is O (n) of ....... We increase letter by letter sequence, once to a relatively "small" letters, because we have to order a minimum dictionary. We want it as high as possible.

So we try to "bubble" like the little Wang Qianmian send. After those larger letters temporary tail thrown away. Assume if they will appear later. Come back will increase. But suppose the letters we passed no longer appeared. Bubbling will stop, this smaller letters was only able to temporarily stop here. This is consistent with our intuition. "The last time a" big letter is practical, because we finally can not be less letters.

The subject is relatively simple, but needs careful thought.

On my code:

#include <cstdio>
#include <cstring>
#include <stack>
#include <string>
#include <vector>
using namespace std;

char s[1024],answer[30];
int main() {
int z;
	for (scanf("%d",&z);z;--z) {
		scanf("%s",s);
		stack<int> st;
		vector<int> num(26,0);
		for (int i = 0; s[i]; ++i) {
			++num[s[i] - 'a'];
		}
		vector<bool> have(26,false);
		for (int i = 0; s[i]; ++i) {
			int c = s[i] - 'a';
			--num[c];
			if (!have[c]) {
				for  (;(!st.empty()) && num[st.top()] && (st.top() > c); have[st.top()] = false,st.pop())
				;
				st.push(c);
				have[c] = true;
			}
		}
		answer[st.size()] = 0;
		for (int i = st.size(); !st.empty(); answer[--i] = st.top() + 'a',st.pop())
		;
		puts(answer); 	
			
	}
	return 0;
}
	

Code also recorded a number of times after the letter appeared, and it is already in the stack. As each letter out of the stack at most once, the complexity is O (n) a.


(9) a special "stack" there is only push_back, push_front pop_back and three operations. Order of the operands is 1-n, gives the final order of the number of pop-up, before asking how the order of operations or impossible.

The problem is the rocket fuel line to see the interview. Because it is face questions. Code is no place to submit ...... they want. Welcome comments.

First, the array is assumed in a number of pop-up inside, and arranged in an array is a 1-n, i.e. an array of a number of not missing. Do not count the number of repeat, no illegal out of range.

Because all of the numbers are sequentially added to it a 1-n, a number x is assumed to be popped, the number x should be less than in the inside. In order of how they are inside it? This and "time stamp" related, first pop-up to be sure the tail. So let's count the number of each "time stamp." Before then pop x, according to the time stamp less than x. Decide which is the number of each push_front or push_back.

Simulation with deque. Finally, look at the tail number that is not what I want on it. I defined a structure op. Which is a pair <int, int> represent the first operand, the second indicates forward, backward, out. In fact the first dimension can be omitted.

Code:

bool solution(vector<int> &a,vector<pair<int,int> > &op) {
int n = a.size();
vector<int> ord(n + 1);
	for (int i = 0; i < n; ++i) {
		ord[a[i]] = i;
	}
	deque<int> q;
	/*
		1 push_back
		-1 push_front
		0 pop_back
	*/
	op.clear();
	for (int i = 1, j = 0;j < n; ++j) {
		for (;i <= a[j]; ++i) {
			if ((q.empty()) || (ord[q.back()] > ord[i])) {
				q.push_back(i);
				op.push_back(make_pair(i, 1));
			}
			else {
				q.push_front(i);
				op.push_back(make_pair(i, 0));
			}
				
		}
		if ((q.empty()) || (q.back() != a[j])) {
			op.clear();
			return false;
		}
		q.pop_back();
		op.push_back(make_pair(a[j], 0));
	}
	return true;
}




Guess you like

Origin www.cnblogs.com/mqxnongmin/p/10959275.html