An approach to Programming Contests with C++
-
Upload
vnit-acm-student-chapter -
Category
Education
-
view
2.306 -
download
5
Transcript of An approach to Programming Contests with C++
![Page 1: An approach to Programming Contests with C++](https://reader033.fdocuments.us/reader033/viewer/2022052908/55945e941a28ab114e8b459f/html5/thumbnails/1.jpg)
![Page 2: An approach to Programming Contests with C++](https://reader033.fdocuments.us/reader033/viewer/2022052908/55945e941a28ab114e8b459f/html5/thumbnails/2.jpg)
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++](https://reader033.fdocuments.us/reader033/viewer/2022052908/55945e941a28ab114e8b459f/html5/thumbnails/3.jpg)
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++](https://reader033.fdocuments.us/reader033/viewer/2022052908/55945e941a28ab114e8b459f/html5/thumbnails/4.jpg)
- 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++](https://reader033.fdocuments.us/reader033/viewer/2022052908/55945e941a28ab114e8b459f/html5/thumbnails/5.jpg)
A world of strings
![Page 6: An approach to Programming Contests with C++](https://reader033.fdocuments.us/reader033/viewer/2022052908/55945e941a28ab114e8b459f/html5/thumbnails/6.jpg)
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++](https://reader033.fdocuments.us/reader033/viewer/2022052908/55945e941a28ab114e8b459f/html5/thumbnails/7.jpg)
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++](https://reader033.fdocuments.us/reader033/viewer/2022052908/55945e941a28ab114e8b459f/html5/thumbnails/8.jpg)
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++](https://reader033.fdocuments.us/reader033/viewer/2022052908/55945e941a28ab114e8b459f/html5/thumbnails/9.jpg)
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++](https://reader033.fdocuments.us/reader033/viewer/2022052908/55945e941a28ab114e8b459f/html5/thumbnails/10.jpg)
- 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++](https://reader033.fdocuments.us/reader033/viewer/2022052908/55945e941a28ab114e8b459f/html5/thumbnails/11.jpg)
#include<sstream>#include<sstream>
![Page 12: An approach to Programming Contests with C++](https://reader033.fdocuments.us/reader033/viewer/2022052908/55945e941a28ab114e8b459f/html5/thumbnails/12.jpg)
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++](https://reader033.fdocuments.us/reader033/viewer/2022052908/55945e941a28ab114e8b459f/html5/thumbnails/13.jpg)
- 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++](https://reader033.fdocuments.us/reader033/viewer/2022052908/55945e941a28ab114e8b459f/html5/thumbnails/14.jpg)
![Page 15: An approach to Programming Contests with C++](https://reader033.fdocuments.us/reader033/viewer/2022052908/55945e941a28ab114e8b459f/html5/thumbnails/15.jpg)
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++](https://reader033.fdocuments.us/reader033/viewer/2022052908/55945e941a28ab114e8b459f/html5/thumbnails/16.jpg)
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++](https://reader033.fdocuments.us/reader033/viewer/2022052908/55945e941a28ab114e8b459f/html5/thumbnails/17.jpg)
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++](https://reader033.fdocuments.us/reader033/viewer/2022052908/55945e941a28ab114e8b459f/html5/thumbnails/18.jpg)
#include<vector>#include<vector>
![Page 19: An approach to Programming Contests with C++](https://reader033.fdocuments.us/reader033/viewer/2022052908/55945e941a28ab114e8b459f/html5/thumbnails/19.jpg)
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++](https://reader033.fdocuments.us/reader033/viewer/2022052908/55945e941a28ab114e8b459f/html5/thumbnails/20.jpg)
So why not Arrays??So why not Arrays??
Variable Size!!Variable Size!!
![Page 21: An approach to Programming Contests with C++](https://reader033.fdocuments.us/reader033/viewer/2022052908/55945e941a28ab114e8b459f/html5/thumbnails/21.jpg)
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++](https://reader033.fdocuments.us/reader033/viewer/2022052908/55945e941a28ab114e8b459f/html5/thumbnails/22.jpg)
Ready to use Algorithms Ready to use Algorithms #include<algorithm>#include<algorithm>
![Page 23: An approach to Programming Contests with C++](https://reader033.fdocuments.us/reader033/viewer/2022052908/55945e941a28ab114e8b459f/html5/thumbnails/23.jpg)
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++](https://reader033.fdocuments.us/reader033/viewer/2022052908/55945e941a28ab114e8b459f/html5/thumbnails/24.jpg)
There’s More..There’s More..
min_element()min_element()max_element()max_element()
next_permutation()next_permutation()prev_permutation()prev_permutation()