-
Notifications
You must be signed in to change notification settings - Fork 11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Organized, a few details added in competitive #5
base: master
Are you sure you want to change the base?
Changes from all commits
35f9f75
7366dd3
6879163
bac4809
1dff54f
7dbed93
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,7 @@ If you have solved 100+ problems and are looking for guidance on | |
how to solve problems involving algorithms and data structures, | ||
this document is not for you.) | ||
|
||
Competitive Programming is an interesting activity which mixes problem solving with programming. | ||
Competitive Programming is an interesting activity which mixes problem-solving with programming. | ||
It is not only enjoyable but also very demanded in placements. | ||
Competitive programming will make you very good at writing efficient programs quickly. | ||
If you get really serious with competitive programming, it will make you an expert in data structures and algorithms. | ||
|
@@ -31,10 +31,14 @@ But at some point of time (especially when you reach advanced stages), | |
you'll need features which most languages have but C does not. | ||
Learning C++ is very easy if you know C. | ||
If you already know C, you should start competitive programming in C and learn C++ in parallel. | ||
> You may refer to [C to C++ for Competitive Programming](c2cpp.md) for a brief intro and resources after you solve [a few basic problems](#making-the-first-step) using C. | ||
|
||
Even if you are not confident of your skills in a programming language, you can (and should) still start. | ||
Competitive programming is also a good way to practice a new language you have learned. | ||
|
||
If you have no prior knowledge you can begin with [CS50 2016](http://cs50.tv/2016/fall/) (week 0 to week 5) | ||
The course teaches C which anyways is useful as it's a part of BITS curriculum. | ||
|
||
## Making the first step | ||
|
||
You'll find problems on many websites. | ||
|
@@ -74,11 +78,16 @@ For each difficulty level, easier problems generally have more submissions. | |
So you can sort problems based on number of submissions to find the easiest ones. | ||
|
||
For beginners, Codechef is a good site. | ||
If you have never before solved a problems on an online judge, you can begin by solving | ||
If you have never before solved problems on an online judge, you can begin by solving | ||
the easiest problem on Codechef - [Life, the Universe, and Everything](https://www.codechef.com/problems/TEST). | ||
You will have to read the [Input/Output tutorial](http://blog.codechef.com/2009/02/24/54/) to solve the problem. | ||
If you face problems, you can refer to [Eklavya's solutions](https://www.codechef.com/status/TEST,sharmaeklavya2). | ||
He has submitted code in many languages, so you'll most likely find a solution in the language of your choice. | ||
Also, Codechef has [screencasts](https://www.youtube.com/playlist?list=PLi0ZM-RCX5ntfqwXRirwA_pcufHinjllG) explaining this problem in C, C++ and Java. | ||
|
||
After this, you can move on to more problems from [Codechef's beginner section](https://www.codechef.com/problems/school?sort_by=SuccessfulSubmission&sorting_order=desc). | ||
These problems require only basic knowledge of programming (arrays, strings, loops) and math/logic. | ||
The problems with the problem-code beginning with `FLOW` are particularly easy and helpful in getting acquainted with submissions on online judges. | ||
|
||
SPOJ is also a good place to start. | ||
There are problems there even for people who are new to programming. | ||
|
@@ -135,14 +144,14 @@ One frequently asked question is what compiler or IDE to use. | |
If you have Linux or Mac, it is best to use: | ||
|
||
* gcc for C | ||
* g++ for C++ | ||
* javac for Java (both oracle and openjdk are good) | ||
* gcc/g++ for C++ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Although it's possible to compile C++ code using |
||
* javac for Java (both Oracle and OpenJDK are good) | ||
|
||
If you are on windows, you might want to use an IDE. | ||
Code::Blocks is good for C and C++. | ||
|
||
There are also some online compilers available. | ||
The most well known is [ideone](https://ideone.com/). | ||
The most well-known is [ideone](https://ideone.com/). | ||
Codechef has an online compiler called [code-compile-run](https://www.codechef.com/ide). | ||
|
||
## Standard library | ||
|
@@ -165,5 +174,6 @@ cplusplus.com has better explanations for these topics. | |
Some people realize very late that C++ offers a [sort function](http://www.cplusplus.com/reference/algorithm/sort/). | ||
This is one of the most used functions in competitive programming. | ||
|
||
Links to documentation of other languages: | ||
* [Python library reference](https://docs.python.org/3/library/index.html) | ||
* [Java library reference](https://docs.oracle.com/javase/8/docs/api/overview-summary.html) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,185 @@ | ||
# C to C++ for competitive | ||
|
||
Author: [Divesh Uttamchandani](https://github.com/diveshuttam) | ||
Contact: diveshuttamchandani@gmail.com | ||
|
||
This article was written keeping competitive in mind and not for using C++ in development or any other context. | ||
The aim here is to get started with important features of C++ as quickly as possible, so skips many details. | ||
Most of the C++ features mentioned here work on C++98 and after but some work only after C++11 so for simplicity we will assume to be using C++11. | ||
Also, we will be using GCC/G++ compiler. | ||
You can also use an IDE like [Code::Blocks](http://www.codeblocks.org/downloads/26) it supports GCC and G++ compiler (Windows users need to download the binary with MinGW). | ||
Remember to enable C++11 or C++14 option in Code::Blocks' settings. | ||
|
||
In this article I have primarily covered 2 things: | ||
1) Porting C's code to C++ | ||
2) A couple of things new/different in C++ w.r.t C and are useful for competitive. | ||
|
||
## Prerequisite | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Change it to 'Prerequisites'. |
||
|
||
We require that you are aware of at least the following concepts in C: | ||
|
||
* Basic Syntax of C | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Everything in this list (loops, arrays, functions) is 'Basic Syntax of C', so remove this point and make 'Basic Input and Output' and 'Operators' first-level points. |
||
* Basic Input and Output | ||
* Operators | ||
* Loops | ||
* Arrays/Strings | ||
* Functions | ||
* Structures | ||
* Pointers | ||
|
||
If you don't know any of the above things, it's better to have a recap first and then come back to this article. | ||
We WON'T be requiring concepts like `enum`, `union`, `static`, file I/O etc. | ||
As long as you are familiar with the ones above you are good to go. | ||
|
||
## Porting C Code | ||
|
||
Once you are familiar with C its easy to migrate to and learn C++. | ||
First of all, since C++ is the superset of C, you can use most of the concepts of C in C++. | ||
All of the things listed in prerequisites remain same in C++. | ||
|
||
You can use C's `scanf` and `printf` in C++ also; they are defined in header file `<cstdio>`. | ||
Similarly, string functions are defined in header file `<cstring>`, math functions in `<cmath>`. | ||
We will be using the line `#include<bits/stdc++.h>` in our code to resolve all issues at once. | ||
`<bits/stdc++.h>` is basically a header file containing all the standard C++ header files (header files for C/C++ , most STL files etc.). | ||
Though it's not a good development practice to include unnecessary headers. It is pretty common in competitive, | ||
as judges use runtime for the time limit and this only increases my compile time and not my runtime. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove 'my's from this sentence. |
||
For more details, you can refer to [GeeksforGeeks](http://www.geeksforgeeks.org/bitsstdc-h-c/) | ||
|
||
Also, we will add another line to our code- `using namespace std;`. | ||
The reason here is related to namespaces but ignore this for now. | ||
However if curious, refer to the links at the [end](#Namespaces) (NOT a recommended read for beginners). | ||
|
||
Another point to keep in mind is that in C++ the return type of main() has to be int. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Put |
||
|
||
Keeping above points in mind, here is a sample program: | ||
|
||
```cpp | ||
#include<bits/stdc++.h> //includes all header files | ||
using namespace std; | ||
|
||
int main() | ||
{ | ||
int t; | ||
scanf("%d",&t); | ||
printf("%d",t); | ||
printf("Hello World") | ||
return 0; | ||
} | ||
``` | ||
To compile this program on linux, use `gcc --std=c++11 filename.cpp -o example` to run use `./example`. | ||
For Code::Blocks use `build and run` in the build menu. | ||
|
||
## New in C++ (useful for competitive) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you should change 'New' to 'Extra', since C++ is a fairly old language. |
||
|
||
### Easier I/O (cin and cout) | ||
|
||
Note: You can refer to [this tutorial](http://www.cplusplus.com/doc/tutorial/basic_io/) for a better overview of I/O in C++, | ||
Just to minimize the number of jumps I have included their basic usage and some useful details here. | ||
|
||
In C++ the input and output are syntactically a bit easier than in C ; for example, refer to the following code: | ||
|
||
```cpp | ||
#include<iostream> | ||
using namespace std; | ||
|
||
int main() | ||
{ | ||
int t; | ||
cin>>t; | ||
cout<<t; | ||
cout<<"Hello world"; | ||
} | ||
``` | ||
This implements the same functionality as the sample C code before this. | ||
It is easier in the sense that you don't need to specify placeholders such as "%d" and "%f" etc. | ||
How to print a variable is judged on the basis of its type. | ||
`<iostream>` here is the header file for `cin` and `cout`. | ||
|
||
If I write `cin>>a;` remember that the input is generally broken at whitespaces(including tabs, newline, space) | ||
i.e consider the following program for example: | ||
|
||
```cpp | ||
#include<bits/stdc++.h> | ||
using namespace std; | ||
|
||
int main() | ||
{ | ||
int a,b; | ||
char Str[10]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you should set a good example for others by not beginning variable names with an uppercase character. Identifiers which begin with an uppercase character are generally used as class names. Identifiers in which all characters are uppercase are generally macros or constants. |
||
|
||
cin>>a>>b>>Str; | ||
cout<<"a="<<a<<"\nb="<<b<<"\nc="<<c; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is |
||
} | ||
``` | ||
|
||
The line `cin>>a>>b>>c;` is es exactly same as `cin>>a;` `cin>>b;` `cin>>c;`. | ||
Similarly for `cout`. | ||
Now if I give the following input: | ||
|
||
``` | ||
1 2 | ||
Hello World | ||
``` | ||
In the real scenario, I will press 1 'space' 2 'enter' Hello 'space' World 'enter' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you need to explain this? |
||
The output of the above program is | ||
``` | ||
a=1 | ||
b=2 | ||
c=Hello | ||
``` | ||
Note: | ||
1. Though the input is broken at whitespaces, `cin>>something;` is not executed while you are typing the input. It is only executed after pressing enter. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't know what you mean by this. |
||
That makes us type in `Hello world` without reaching `return 0;`. | ||
2. Multiple whitespaces between the input are ignored while using cin. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think we need to explain points like this, because |
||
3. In case of type `char`, the input is broken at each character itself. Though for type char* / char[] input is again broken at spaces. | ||
Coding a few sample programs will help in understanding the working of `cin` in more detail. | ||
4. Although `cin` and `cout` are easier than `scanf` and `printf`, as such they are relatively slow. This may lead to TLE on certain problems. | ||
There are 2 ways to resolve this | ||
1. Use `scanf` and `printf` instead. | ||
2. Add the line `ios::sync_with_stdio(false)` in begining of `main()` and then use `cin` and `cout` ONLY. For more details refer to [this quora question](https://www.quora.com/Is-cin-cout-slower-than-scanf-printf) | ||
|
||
|
||
### Standard Template Library | ||
C++ has many new features including classes and templates. | ||
Although these are great and interesting concepts, for most of our part we can work without knowing their details. | ||
The main advantage of C++ for competitive is its extensive range of inbuilt "functions" offered through Standard Template Library (STL). | ||
These include implementations of various Algorithms (sorting, binary search etc.) and Data Structures(stacks, queues, hash map etc.). | ||
|
||
Although STL is built upon templates, one doesn't need to know much of templates/classes to use it. | ||
Just a basic overview of __What is a class__ and __What is a template__ will suffice. | ||
You may refer to the links given at the end for an introduction to these concepts. They are enough for our purposes. | ||
Concepts related to OOP like inheritance, polymorphism etc. are never used. | ||
I recommend going through these links about classes and templates after you have read the tutorials linked in the paragraph below. | ||
This is to ensure that you have a bit of a context of their actual usage. | ||
|
||
A good resource for getting started with STL is Topcoder tutorials | ||
([PART I](https://www.topcoder.com/community/data-science/data-science-tutorials/power-up-c-with-the-standard-template-library-part-1/) | | ||
[PART II](https://www.topcoder.com/community/data-science/data-science-tutorials/power-up-c-with-the-standard-template-library-part-2/) ). | ||
They teach how to use STL in context of competitive. | ||
Since STL was added later to C++, Its syntax may seem a bit unusual at first. | ||
It gets easier after you use it in a few programs. | ||
There is no need to remember the exact prototype of functions in STL as you can always refer to the following links: | ||
* [STL Docs](https://www.sgi.com/tech/stl/) | ||
* [cppreference](http://en.cppreference.com/w/) | ||
* [cplusplus.com](http://www.cplusplus.com/reference/stl/) | ||
|
||
Even in final rounds of prestigious contests, access to some documentation is always provided. | ||
Once you have some idea of STL, you can read the [links about classes and templates](classes-and-templates). | ||
Then have a look at the Topcoder tutorials again. This generally helps in getting started easily. | ||
After this, you are pretty much dependent on documentation and google searches | ||
|
||
#### Few google searches which may be helpful in learning more new features: | ||
|
||
* auto specifier C++11 | ||
* range-based loop C++11 | ||
* strings in C++ | ||
* unordered map in C++ | ||
|
||
#### Namespaces | ||
|
||
http://www.geeksforgeeks.org/namespace-in-c/ | ||
|
||
#### Classes and Templates | ||
|
||
http://www.geeksforgeeks.org/c-classes-and-objects/ | ||
http://www.geeksforgeeks.org/templates-cpp/ |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
This file has links for various articles in this repository | ||
|
||
## Backend | ||
* [Learning Django](./backend/django.md) | ||
|
||
## Competitive | ||
* [Starting out with Competitive Programming](./competitive-programming/CP1.md) | ||
* [ACM-CPSIG session 2015](./competitive-programming/CPSIG.md) | ||
* [C to C++ for Competitive](./competitive-programming/c2cpp.md) | ||
|
||
## Linux | ||
* [Summary of the introductory session on Linux 2015](./linux/session-summary.md) | ||
* Using Apt | ||
* [Part 1](./linux/package-management/p1.md) | ||
* [Part 2](./linux/package-management/p2.md) | ||
* [Part 3](./linux/package-management/p3.md) | ||
|
||
## Tools | ||
* [Tools for developers](./tools/tools1.md) | ||
* [Git](./tools/git.md) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you should remove the
>
symbol from the beginning of the line.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Split this sentence into 2 lines.