1004. Simple Stack Application
|
configuration:
#include <iostream>
#include <algorithm>
#include <vector>
#include <cctype>
#include <cstdlib>
#include <list>
#include <stack>
using namespace std;
1001. The first quartile
In descriptive statistics, the quartiles (四分位数) of a ranked set of data values are the three points that divide the data set into four equal groups, each group comprising a quarter of the data. A quartile is a type of quantile. The first quartile (Q1)(第一四分位数) is defined as the middle number between the smallest number and the median (中位数) of the data set. The second quartile (Q2) is the median of the data. The third quartile (Q3) is the middle value between the median and the highest value of the data set. For example, the ordered data set: (6, 7, 15, 36, 39, 40, 41, 42, 43, 47, 49), the first quartile Q1 = 15, the median Q2 = 40, and Q3 =43.
Now your task is to compute the first quartile and its location for a given data set. The input data set is a vector v, which is not necessary ordered. For example, v=(1,2,3,2,3,2,4), you will sort the data set and get v = (1,2,2,2,3,3,4), then the first quartile is located at k =[0.25*n](taking the whole number, where n is the number of items in the data set). In this case, the first quartile is located at k =[0.25*7] = 1, so the first quartile is v[1] = 2.
pair<double, int> Q1(vector<double> v)
//input: v is a nonempty data set
//output: the first quartile and its index at v. For example, for data set v=(1,2,3,2,3,2,4), the result is (2,1).
pair<double, int> Q1(vector<double> v){
//sort
sort(v.begin(),v.end());
int q1 = v.size() * 0.25;
double d = v[q1];
pair<double, int> p = pair<double, int>(d,q1);
return p;
}
Implement the following function which reads the probability distribution and computes the mean (期望值) for a discrete random variable.
double mean(){
//read input, compute the mean and return it.
}
The first line is an integer n (0<n<100), the number of values which a discrete random variable X can take. Then follows n integers which are the values X can take, then n doubles which are the corresponding probability.
3 0 1 2 0.4 0.2 0.6
1.4
Your function mean() simple reads data from standard input, computes the mean and returns it. Don't print anything. Submit only your function mean().
double mean(){
int n;
cin>>n;
vector<int> values;
vector<double> probabilities;
int t;
for (int i = 0; i < n; ++i)
{
cin>>t;
values.push_back(t);
}
double tt;
for (int i = 0; i < n; ++i)
{
cin>>tt;
probabilities.push_back(tt);
}
double mean = 0;;
for (int i = 0; i < n; ++i)
{
mean += values[i] * probabilities[i];
}
return mean;
}
1003 Set Insection
A set of integers is repsented by an ordered list of integers in decsending (递减) order. Given two sets, your task is to compute the intersection(交).
There are multiple test cases.
The first line is the number of test cases. Then follow the test cases. For each test case, the first line is the number of elements in the first set, then the next line contains the elements in descending order, the third line is the number of elements in the second set, then follows the elements of the second set in descending order.
For every test case, output all elements in descending order and leave one space after each element. Every output takes one line.
3 3 5 3 2 2 6 3 2 4 2 1 3 4 9 6 3 2 3 9 5 2
3 9 2
//Set intersection
list<int> intersection(list<int> &l, list<int> &m){
list<int> result;
while(!l.empty() && !m.empty()){
if(l.front() == m.front()){
result.push_back(l.front());
l.pop_front();
m.pop_front();
}
else if(l.front()>m.front()){
l.pop_front();
}
else{
m.pop_front();
}
}
return result;
}
void print(list<int> & l){
for (std::list<int>::iterator i = l.begin(); i != l.end(); ++i)
{
cout<<*i<<" ";
}
cout<<endl;
}
int main(){
int numOfCases;
cin>>numOfCases;
for (int eachCase = 0; eachCase < numOfCases; ++eachCase)
{
int numset1;
cin>>numset1;
list<int> set1,set2;
int t;
for (int i = 0; i < numset1; ++i)
{
cin>>t;
set1.push_back(t);
}
int numset2;
cin>>numset2;
for (int i = 0; i < numset2; ++i)
{
cin>>t;
set2.push_back(t);
}
list<int> result = intersection(set1,set2);
print(result);
}
}
Description
//pop out all strings in s and print them on the screen one by one in one line. Leave one space after each string, and add a nelwline after the last string (and the space). //insert here }
void print_it(stack<string> s){
while(!s.empty()){
cout<<s.top()<<" ";
s.pop();
}
cout<<endl;
}
|
string find(vector<vector<pair<int, string> > > t, int k)
//v[i] stores all records whose hashing addresses are i.
{
// use hash function h(k) = k%t.size()
int index = k%t.size();
vector<pair<int, string> v = t[index];
for (int i = 0; i < v.size(); ++i)
{
if(v[i].first == k){
return v[i].second;
}
}
//insert here
return "";
}
1006. Look up problem
| ||
Description
Your task if to look up a string w in a set of words S, and returns true if w belong S and false otherwise.
Input
The first line is an integer n (less than 120000), the number of words in the set, then n words are followed. The next line is an integer m(less than 120000), which is the number of words you will look up in the set of words given above. All the words are not necessarily ordered.
Output
For each of the m words, if the search is successful, print ‘Y', otherwise print 'N'.
Sample Input
4 algortihm design stack complexity 5 data design stacks vector sign
Sample Output
NYNNN
Hint
You need a logarithmic algorithm. |
//look up problem
void Bsearch(vector<string> & orderedSet, string & w, int & position){
string data;
int bottom = 0,top = orderedSet.size()-1;
while(bottom <= top){
position = (bottom + top)/2;
data = orderedSet[position];
if(data == w) return;
if(data < w) bottom = position + 1;
else top = position - 1;
}
position = -1;
}
int main(){
int n;
cin>>n;
vector<string> v;
string s;
for (int i = 0; i < n; ++i)
{
cin>>s;
v.push_back(s);
}
sort(v.begin(), v.end());
int m;
cin>>m;
std::vector<string> l;
for (int i = 0; i < m; ++i)
{
cin>>s;
l.push_back(s);
}
for (int i = 0; i < m; ++i)
{
int position;
Bsearch(v,l[i],position);
if(position>=0)
cout<<"Y";
else cout<<"N";
}
}
1007. Boolean expression equivalence
| ||
Description
Given two boolean expressions and an assignment to all the variables in the expressions, you will check if they compute to the same value. The boolean expressions are given by postfix forms, which contain only lower case letters for variables and operation signs: ~ for negation, + for disjunction (|| in C++) and * for conjunction (&& in C++). The asignment is give by a map which maps characters to booleans. Now your task is to implement the following function: bool equal(map<char, bool> &f, const string &e1, const string &e2) where strings e1 and e2 are two boolean expressions in postfix form and f is the substitution. The function returns true if e1 and e2 compute to the same value under f, returns false otherwise. For example, e1="ab+~", e2="b~a*", f['a']=false, f ['b']=true, both e1 and e2 compute to false, so equal(f, e1,e2) is true. If e1="ab+", e2="ab*“, f['a']=true, f['b']=false, then e1 and e2 compute to true and false respectively, so equal(f,e1,e2) is false in this case.
Input
Submit only your function evaluate(). |
//Boolean expression equivalence
bool equal(map<char, bool> &f, const string &e1, const string &e2){
stack<bool> s1,s2;
for (int i = 0; i < e1.size(); ++i)
{
char c = e1[i];
if(islower(c)) s1.push(f[c]);
if(c == '~'){
bool t = !s1.top();
s1.pop();
s1.push(t);
}
if(c=='+'){
bool t1 = s1.top();
s1.pop();
bool t2 = s1.top();
s1.pop();
t1 = t1 || t2;
s1.push(t1);
}
if(c == '*'){
bool t1 = s1.top();
s1.pop();
bool t2 = s1.top();
s1.pop();
t1 = t1 && t2;
s1.push(t1);
}
}
//e2
for (int i = 0; i < e2.size(); ++i)
{
char c = e2[i];
if(islower(c)) s2.push(f[c]);
if(c == '~'){
bool t = !s2.top();
s2.pop();
s2.push(t);
}
if(c=='+'){
bool t1 = s2.top();
s2.pop();
bool t2 = s2.top();
s2.pop();
t1 = t1 || t2;
s2.push(t1);
}
if(c == '*'){
bool t1 = s2.top();
s2.pop();
bool t2 = s2.top();
s2.pop();
t1 = t1 && t2;
s2.push(t1);
}
}
return s1.top() == s2.top();
}
1008. Buiding a binary search tree
| ||
Description
Given a list of integers, we can build a binary search tree by inserting them one by one starting from the empty tree. Could you compute the height of the tree? The heigth of then empty binary search tree is -1, and hence the singltong binary tree has height 0.
Input
The first line is the number of test cases. Then follows the test cases. Each test case has two line: the first one is the number of integer keys, the second line list n distinct integers separated with spaces.
Output
For each test case, output the height on one line. It is guaranteed that all the input and output are ints.
Sample Input
2 3 3 5 2 4 7 5 4 9
Sample Output
1 2 |
//building a binary searchj tree
struct BinaryNode{
int data;
BinaryNode *left, *right;
BinaryNode(T d, BinaryNode *l=NULL, BinaryNode* r=NULL):data(d), left(l), right(r) {};
};
int height(BinaryNode* root){
int leftheight,rightheight;
if(root==NULL) return -1;
else{
leftheight = height(root->left);
rightheight = height(root->right);
return (leftheight>rightheight)? (leftheight+1) : (rightheight+1);
}
}
void recursive_insert(BinaryNode* &p, int data){
if(p == NULL){
p = new BinaryNode(data);
}
else if(data < p->data){
recursive_insert(p->left,data);
}
else if(data > p->data){
recursive_insert(p->right,data);
}
}
void BST_insert(BinaryNode* &root,int data){
recursive_insert(root, data);
}
int main(){
int cn;
cin>>cn;
for (int eache = 0; eache < cn; ++eache)
{
int n;cin>>n;
int t;
BinaryNode* root = NULL;
for (int i = 0; i < n; ++i)
{
cin>>t;
BST_insert(root,t);
}
cout<<height(root)<<endl;
}
}