Algorithmic Race Primer Classic 5.2 STL Preliminary

Source: Internet
Author: User

1. Sort and retrieve, learn to use sort sort, and low_bound function

Raju and Meena love-to-play with marbles. They has got a lot of marbles with numbers written on them. At the beginning, Raju would place the marbles one after another in ascending order of the numbers written on them. Then Meena would ask Raju to find the first marble with a certain number. She would count 1...2...3. Raju gets one point to correct answer, and Meena gets the point if Raju fails. After some fixed number of trials the game ends and the player with maximum points wins. Today it ' s your chance to play as Raju. Being the Smart kid, you ' d be taking the favor of a computer. But don ' t underestimate Meena, she had written a program to keep track how much time you ' re taking to give all the answers . So now that you had to write a program, which would help you in your role as Raju.

Input

There can multiple test cases. Total no of test cases was less than 65. Each test case consists begins with 2 integers:n the number of marbles and Q the number of queries Mina would make. The next n lines would contain the numbers written on the N marbles. These marble numbers won't come in any particular order. Following q lines would have Q queries. Be assured, none of the input numbers is greater than 10000 and none of them are negative. Input is terminated by a test case where N = 0 and Q = 0.

Output

For each test case output, the serial number of the case. For each of the queries, print one line of output. The format of this line would depend upon whether or not the query number was written upon any of the marbles. The different formats is described below:? ' x found at Y ', if the first marble with number x is found at position y. Positions is numbered 1, 2, ..., N.? ' X not found ', if the marble with number X was not present. Look at the output for sample input for details.

Sample Input

4 1 2 3 5 1 5 5 2 1 3 3 3 1 2 3 0 0

Sample Output

case# 1:5 found at 4

case# 2:2 not found 3 found at 3

#include <iostream>#include<stdio.h>#include<string>#include<string.h>#include<algorithm>#include<map>#include<Set>#include<queue>#include<stack>using namespacestd;Const intMAXN =10005;intn,q;inttemp;intCasee=1;intA[MAXN];intMain () { while(cin>>n>>Q) {if(n==0&&q==0)             Break; Else{cout<<"case#"<<casee++<<":"<<Endl;  for(intI=0; i<n;i++) Cin>>A[i]; Sort (A,a+N);//Sort, you can write your own CMP functions as parameters, usually used in the structure of the order.  while(q--) {cin>>temp; intFlag=lower_bound (a,a+n,temp)-a;//looking for x in an already sorted arrayif(a[flag]==temp) cout<<temp<<"found at"<<flag+1<<Endl; Elsecout<<temp<<"Not found"<<Endl; }        }    }    return 0;}

2. Use of vectors.

Common functions, push_back () trailing elements pop_back () remove the last element size () returns the length resize (b) changes size, preserving the element between subscript 0-b

Reverse (Vec.begin (), Vec.end ()); Flips the elements, that is, in reverse order!

Vector element traversal using iterators for (vector<int>::iterator it = V.begin (); It!=v.end (); it++)

              {cout<<*it<<""; }

UVA-101

Topic Link https://vjudge.net/problem/UVA-101

#include <iostream>#include<stdio.h>#include<string>#include<string.h>#include<algorithm>#include<map>#include<Set>#include<queue>#include<stack>#include<vector>using namespacestd;Const intmaxn= -; Vector<int>V[MAXN];intN;strings1,s2;intb;voidFINDD (intAint&p,int&h) {     for(p=0;p <n;p++)    {         for(h=0; H<v[p].size (); h++)        {            if(v[p][h]==a)return; }    }}voidFUN1 (intPintH//return to position{     for(inti=h+1; I<v[p].size (); i++)    {        intj=V[p][i];    V[j].push_back (j); } v[p].resize (H+1);}voidFun2 (intP1,intHintp2) {     for(intI=h;i<v[p1].size (); i++) V[p2].push_back (V[p1][i]); V[p1].resize (h);}intMain () {CIN>>N;  for(intI=0; i<n;i++) V[i].push_back (i);  while(cin>>S1) {        if(s1=="quit")             Break; CIN>>a>>s2>>b; intPA,HA,PB,HB;        FINDD (A,pa,ha);        FINDD (B,PB,HB); if(pa==pb)Continue; //cout<<pa<< "<        if(s2=="onto") fun1 (PB,HB); if(s1=="Move") fun1 (Pa,ha);    Fun2 (PA,HA,PB); }     for(intI=0; i<n;i++) {cout<<i<<":";  for(intj=0; J<v[i].size (); j + +) cout<<" "<<V[i][j]; cout<<Endl; }    return 0;}

Algorithmic Race Primer Classic 5.2 STL Preliminary

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.