Interesting questions related to sequences 4. Interesting questions related to Sequences

Source: Internet
Author: User

Interesting questions related to sequences 4. Interesting questions related to Sequences

(8) If an English word is given, duplicate letters are eliminated. Only letters can be deleted, and the alphabetic order cannot be exchanged. Each letter in the original word appears only once, and the result with the smallest Lexicographic Order is obtained.

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

I like this question very much. The clever thing is that its algorithm is O (n ...... . We add a letter and a letter to the sequence, and once a relatively "small" letter comes, because we need to have the smallest dictionary order, and we want it to be as advanced as possible. So we try to "bubble" the small ones to the front, and temporarily discard those large letters at the end. If they will appear later, they will be added later. However, if the letter we passed does not appear again, the bubble will stop, and the small letter can only be temporarily stopped here. This is in line with our intuition that the "last appearance" of big letters is useful, because we can not ultimately reduce the number of letters. The question is relatively simple, but you need to think carefully.

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;}

The Code records the number of times after a letter and whether it is already in the stack. Because each letter can enter and exit the stack at most once, the complexity is O (n.


(9) A special "stack" contains only push_back, push_front, and pop_back operations. The order of the operands is 1-N, and the order of the last pop-up number is given, ask the sequence of the previous operations or what is impossible at all.

This is an interview with rocket fuel on the Internet. Because it is an interview question, there is no place to submit ...... The Code is also your own idea. Comments are welcome. First, let us assume that the number popped up is in array a, and array a is an arrangement of 1-N. That is, array a has no shortage of a few, no duplicates, and no invalid number out of range.

Because all numbers are added in the order of 1-n. If a number x is first popped up, the number smaller than x should be in it. What is the order of them in it? This is related to the "timestamp". It must be at the end of the pop-up. Therefore, we need to calculate the "timestamp" of each number first, and then before x is displayed, based on the timestamp smaller than x, determine whether the number is push_front or push_back. I used a dual-end queue to simulate it. Finally, let's see if the number at the end of the team is what I want. I have defined a STRUCTURE op, which contains the first pair <int, int> representing the operand, and the second representing forward, backward, and outbound. 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_front0 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;}





Mathematical questions and answers in grade 4

1. Xiaohua's father can cut five of his nails in one minute. How many nails can he cut in five minutes?
2. Xiaohua took 50 yuan to the store to buy a 38 yuan car, but the salesman only gave him 2 yuan. Why?
3. Xiaojun said: "I went fishing yesterday and caught a half-cut fish with no tail, no head, and no head. How many fish have I caught ?" Guess how many fish have Xiaojun caught?
4.6 horses and a big car ran six miles. How many miles per horse ran? How many miles have the six horses run?
5. A puppy tied to a trunk is greedy for a bone on the ground, but the rope is not long enough, and the difference is 5 cm. How can you teach a puppy to hold the bones?
6. Wang went to the second place from the second place. One minute later, Li went to the second place from the second place. When Wang and Li are on the way, which one is farther away from JiaDi?
7. The clock has just been knocked 13 times. What should you do now?
8. On the vast lawn, a cow is grazing. This ox eats half of the grass for a year. Q: How many years does it take to eat all the grass on the grass?
9. My mother has 7 pieces of sugar and wants to divide them into three children equally, but she does not want to cut the remaining sugar. What should she do?
10. There is a row of trees beside the park. Each tree is 3 meters apart. How many meters are there between the first and sixth trees?
11. divide 8 into two halves by the following method. What is the difference between each half? The average arithmetic mean is ____, the center is ____, and the center is ____.
12. There are four corners in a house, and one has a cat. There are three cats in front of each cat. How many cats are there in the room?
13. There are four horns in a house, and one has a cat. There are four cats in front of each cat. How many cats are there in the room?
14. Xiaojun, XiaoHong, and Xiaoping played three games in total. How many games have they played? (Two players play each game)
15. Xiao Ming and Xiao Hua each have a bag of sugar, but I don't know how many pieces of sugar are in each bag. I only know that Xiao Ming gave Xiao Hua 8 blocks, and then Xiao Hua gave Xiao Ming 14 blocks. At this time, there were exactly the same amount of sugar in the bag. Students, who did you say had a lot of sugar? How many more?

Answer:

1.20, including fingernails and fingernails

2. Because he paid the salesman 40 yuan, he only paid him 2 yuan;

3.0 because the fish he caught does not exist;

4.6 miles, 36 miles;

5. You just need to teach the puppy to turn around and grasp the bones with his feet.

6. When they met, they were in the same place, so they were just as far away from JiaDi;

7. The clock should be repaired;

8. It will never eat up the grass, because the grass will grow continuously;

9. Mom eats one item and then gives it to each child;

10.15 meters;

11.4.

12.4;

13.5;

14.2 disks;

15. It turns out that little sugar is more than 6; 14-8 = 6, because the number of sugar blocks for six more people is exactly the same, so it turns out that little sugar is 12 more than James.

A Data Sequence ak (table 30) is known, and a system is formed through the fourth type of response, as shown in Figure 30.

Pre-encoding: A (K) modulo 2 addition (K-2) equal to B (K)
B (K) bipolar conversion, and then related Encoding
Correlation code: B (K)-B option (K-2) is equal to c (thousands)
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.