+ All Categories
Home > Education > An approach to Programming Contests with C++

An approach to Programming Contests with C++

Date post: 02-Jul-2015
Category:
Upload: vnit-acm-student-chapter
View: 2,306 times
Download: 5 times
Share this document with a friend
24
Transcript
Page 1: An approach to Programming Contests with C++
Page 2: An approach to Programming Contests with C++

A lil Bit About Templates B4 V A lil Bit About Templates B4 V BeginBegin

So, What Are They ???So, What Are They ???It Aids Generic Programming

( Basically, Makes Life Much Easier )

You Can Create a Queue Of Type T where, T is of any type !!!

Eg. queue<int> iq;queue<string> sq;

Page 3: An approach to Programming Contests with C++

File HandlingFile Handling

Handling Input, Output From Files Is Handling Input, Output From Files Is Much Easier Than You Think !!!Much Easier Than You Think !!!

freopen(“input file name”,”r”,stdin);freopen(“input file name”,”r”,stdin);freopen(“output file name”,”w”,stdout);freopen(“output file name”,”w”,stdout);

Page 4: An approach to Programming Contests with C++

- TIP -

- TIP -

string s;string s;getline(cin , s , '\n');getline(cin , s , '\n');

Use this to read a whole Use this to read a whole line line of text from the input fileof text from the input file

Page 5: An approach to Programming Contests with C++

A world of strings

Page 6: An approach to Programming Contests with C++

FindFindSimplest way to find a substring

What Does It Do ?What Does It Do ?Returns The Index Of The First Occurrence Of The Substring.

If There Is No Such Occurrence, returns a value > Length Of String.

string a=“hello world”;cout<<a.find(“0 w”);

Page 7: An approach to Programming Contests with C++

ReplaceReplaceAn Easy Way To Replace Text In Strings

What Does It Do ?What Does It Do ?

Replaces Some Particular Text In A String With The Text You Want

To Replace With

string a=“hello world”;cout<<a.replace(i,j,”hi”);

Page 8: An approach to Programming Contests with C++

Other Useful String FunctionsOther Useful String Functions

string s=“01236789876543210”; string s=“01236789876543210”;

s.insert(i,”45”);s.insert(i,”45”);s.erase(i,j);s.erase(i,j);s.length();s.length();s.rfind(“3”);s.rfind(“3”);

Page 9: An approach to Programming Contests with C++

Extract Data From A String With Extract Data From A String With sscanf()sscanf()

It is like scanf() but, scans from a string It is like scanf() but, scans from a string instead of standard input !!!instead of standard input !!!

string s=“12:40”;string s=“12:40”;int h,m;int h,m;sscanf(s.c_str() , "%d:%d“ , &h , &m);sscanf(s.c_str() , "%d:%d“ , &h , &m);cout<<h<<endl<<m;cout<<h<<endl<<m;

Page 10: An approach to Programming Contests with C++

- TIP -

- TIP -

You can use find and replace You can use find and replace together, to replace all occurences together, to replace all occurences of a particular substring in a string, of a particular substring in a string, with some other sequence of with some other sequence of characters, irrespective of the size characters, irrespective of the size of the new sequence of characters ! of the new sequence of characters ! Very Useful !!!Very Useful !!!

Page 11: An approach to Programming Contests with C++

#include<sstream>#include<sstream>

Page 12: An approach to Programming Contests with C++

Converting Converting AnythingAnything Printable, To A Printable, To A StringStringNot as hard as it sounds !!Not as hard as it sounds !!

stringstream ss;string s;float f=1.732;int i=43;char c=‘g’;

ss<<“StringstreamsRock!!”<<f<<i<<c;ss>>s;cout<<s;

Page 13: An approach to Programming Contests with C++

- TIP -

- TIP -

It Is A Better Idea To Use--It Is A Better Idea To Use--

getline(ss , s , ‘\n’);getline(ss , s , ‘\n’);

(or)(or)

s=ss.str();s=ss.str();

Page 14: An approach to Programming Contests with C++
Page 15: An approach to Programming Contests with C++

a^=b^=a^=b;a^=b^=a^=b;

What Does It Do ??What Does It Do ??Guesses Permitted !!Guesses Permitted !!

Page 16: An approach to Programming Contests with C++

a=a^b;a=a^b;b=a^b;b=a^b;a=a^b;a=a^b;

After Simplifying It A Little BitAfter Simplifying It A Little Bit

Page 17: An approach to Programming Contests with C++

It Works Coz Of The Fact It Works Coz Of The Fact That:That:

a^b=ca^b=c =>=> b^c=a b^c=a c^a=bc^a=b

Page 18: An approach to Programming Contests with C++

#include<vector>#include<vector>

Page 19: An approach to Programming Contests with C++

Capabilities:Capabilities:

Easy Sorting!!Easy Sorting!!Constant Access Time!!Constant Access Time!!Linear Time for finding Linear Time for finding

elements!!elements!!

Page 20: An approach to Programming Contests with C++

So why not Arrays??So why not Arrays??

Variable Size!!Variable Size!!

Page 21: An approach to Programming Contests with C++

Useful Functions:Useful Functions:

push_back()push_back()pop_back()pop_back()

insert()insert()erase()erase()clear()clear()

assign()assign()

Page 22: An approach to Programming Contests with C++

Ready to use Algorithms Ready to use Algorithms #include<algorithm>#include<algorithm>

Page 23: An approach to Programming Contests with C++

C++ Already Contains: C++ Already Contains: (Used with respect to (Used with respect to Vector v1)Vector v1)

sort() sort() stable_sort()stable_sort()

qsort()qsort()find()find()

reverse()reverse()merge()merge()

Page 24: An approach to Programming Contests with C++

There’s More..There’s More..

min_element()min_element()max_element()max_element()

next_permutation()next_permutation()prev_permutation()prev_permutation()


Recommended