Tuesday, 26 June 2012

Really Fast I/O methods for Programming in C++ (Input Methods)..

In programming minimizing the runtime of your program is very necessary as the time limit of the problems is very strict..

Many times I have come across the warning – “Be Careful .. Large Input Output”…
In such problems the optimization in the input/output method really helps in giving a better runtime…
After surfing a lot on the web and reading about the different methods, I made a template for myself, which I use in all my problems and sometimes it help me in getting on the 1st page of best solutions ;)

 You can check all the Fast I/O methods on - www.spoj.pl/problems/INTEST (Input test)
                                                www.spoj.pl/problems/INOUTEST (Input + Output Test)

Due to simplicity most of C++ programmers use cin/cout for the Input/Output...
But when the testcases are large cin/cout doesn't work.. You can try it on the above problems...
A solution with cin/cout will time out...

So one of the alternate is the scanf/printf which is nearly 3 times faster than the cin/cout...
An Accepted solution of the problem INTEST is

__________________________________________________________________________________

#include<stdio.h>
int main()
{
 int n,k;
 scanf("%d%d",&n,&k);
 int cnt=0;
 while(n--)
 {
  int num;
  scanf("%d",&num);
  if(num%k==0)cnt++;
 }   
 printf("%d",cnt);
 return 0;
}

__________________________________________________________________________________
But its runtime is 5.52 seconds which is not very good...
So we search for other alternates...

Another faster input handling method is the getchar_unlocked()...
getchar_unlocked is thread unsafe as it locks the screen while getting the input.. But using it on the spoj server will not harm your screen :D....
It is easy to implemet and is very fast...
We can use getchar_unlocked to handle integers and strings faster...

An Implementation of getting integer input through getchar_unlocked() -
Accepted INTEST Solution
 _______________________________________________________________________________


#include<iostream>
#include<cstdio>
#define gc getchar_unlocked

void scanint(int &x)
{
    register int c = gc();
    x = 0;
    for(;(c<48 || c>57);c = gc());
    for(;c>47 && c<58;c = gc()) {x = (x<<1) + (x<<3) + c - 48;}
}

int main()
{  

    int n,k;
    scanint(n);
    scanint(k);
    int cnt=0;
    while(n--)
    {
     int num;
     scanint(num);
     if(num%k==0)cnt++;
    }   
    printf("%d",cnt);
    return 0;            
}

__________________________________________________________________________________

And the runtime of this solution is  0.90 secs.. which is much faster than the scanf...

Registers are faster than memory to access, so the variables which are most frequently used in a C program can be put in registers using register keyword...

This implementation was for positive integers.. Similarly you can make one for the +/- integers..

___________________________________________________________________________________

void scanint(int &x)
{
    register int c = gc();
    x = 0;
    int neg = 0;
    for(;((c<48 || c>57) && c != '-');c = gc());
    if(c=='-') {neg=1;c=gc();}
    for(;c>47 && c<58;c = gc()) {x = (x<<1) + (x<<3) + c - 48;}
    if(neg) x=-x;
}

___________________________________________________________________________________

So this is a very easy to implement fast Input handling method....

We have another fast Input method that is - fread or fread_unlocked.......
There isn't much difference between fread and fread_unlocked as in both the whole input file is first taken in an input buffer and then processed.. Main difference is the locking of screen in fread_unlocked...

Implementaion of fread in INTEST ...
 __________________________________________________________________________________

#include<cstdio>
#include<iostream>
#include<cstdlib>
using namespace std;
#define size 65536
int main()
{
   
    char b[size];
    int t=0,n,k,cnt=0;
    int c,j;
    scanf("%d %d\n",&n,&k);
    while((c = fread(b,1,size,stdin))>0)
    {
               for(j=0;j<c;j++)
               {
                if(b[j]=='\n')
                {
                              if(t%k==0)cnt++;
                              t = 0;
                }
                else
                {
                              t = (t*10) + (b[j]-'0');               
                }             
               }
     }
    printf("%d\n",cnt);   
    return 0;
}

__________________________________________________________________________________

Now this method is more faster as the runtime of this one is 0.65 secs...
Using fread_unlocked increased the runtime to 0.78 secs...
So better use fread()....

But I found getchar_unlocked() much easier to manipulate and code.. So that's my choice...
These were few Input methods that I found useful.. For string handling gets() and fgets() are also good....

If there are other Input handling methods, please let me know....

I will discuss the output methods with the implementations in my next Blog....... :):)

Till then..
 Love and Live..
As Life is Small.. :) :)

Programming Addiction

Programming is like an addiction..  ( Not for everyone :P)
Once you start programming, its like you want to learn more and read more...
It all started with me when I was in my first year...
My senior used to tell me about some of the programming sites where there are lots of interesting
puzzles and questions to solve like SPOJ , Topcoder, CodechefProject Euler etc..
So I started exploring them.. And from then onwards I haven't stopped..
It's like daily you come across different algorithms...
Yeah ALGORITHMS - It is such a heavy word.. and always need space and time..
So whenever you will see the word Algorithm, there will be two other words or complex words
- "TIME Complexity" & the "SPACE Complexity"...
And yes Along with them there are O (the Oh Notation), Omega Notation and others....

Initially when I started Programming, I never cared about the time and memory my program takes ( Cause I used to do the easier Problems ;) )... But after some time me and my friends started learning new things.. (As no more easier questions left for us..)...

We came to know that whatever we were doing unknowingly was part of the greedy algorithms....
And then we read about them... Then the dynamic programming, Number theory, Data structures and many many things... With every single algo I go through there were several optimized versionss..... So just read, learn and implement... That's what I love to do.......

 Now I am about 1 and half years old to programming.....
And there's a lot of things that I have to learn.... But from now on I will love to share whatever I will learn :)

So lOve Coding & lOve Life...
BCoz Life is small... :) :)

Saturday, 9 June 2012

10 Web Designing Tips, Tricks and Techniques

 http://www.getwebdesignhelp.com/wp-content/uploads/2011/02/Web-design-2011.jpg
The process of ideating, planning and creating a website is called web designing and developing. The languages such as HTML, CSS, JavaScript etc are used for structuring, presentation and increase interactivity of the websites. In short this process of web designing is conceptualizing the website from scratch to end.
A professional website is need of the day for every business, it is the success route for a firm or business, if you do not have a professional website a number of clients might not come back after there first visit on your site. However every designer cannot design such a website until he is familiar with the tips, tricks and techniques of the web designing.
Although tutorials provided on the web do give the overview of HTML and web designing but unfortunately that is about it, only overview. However in today’s competitive world only overview is not enough, the web designer needs to know some useful tips, handy tricks and smart techniques.
Although HTML is the most important language but if you want to give your website an extra edge then you should also know CSS and JavaScript as these special coding languages add extraordinary effects to your web designs.
The web design tips provide a wealth of information to assist you in many aspects of web site design. You will find web design tips and advice, HTML tips and codes, CSS codes and JavaScript codes to assist you in creating special effects within your web pages.

The below mentioned tips, tricks and techniques of web designing should be known by all the designers out there:

1. Learn About All Markup Languages

Stay updated with all the markup languages, frameworks and methodologies, this helps the web designer to stay on top of their game. To do so, web designer must continue learning about the latest technologies. If you are a back-end developer then JavaScript and Node.js and if you are a front-end developer then CSS3 and HTML5 should be your forte.

2. Stay sharp on what you already know



This refers to improving in your use of your daily tools. I know every one is comfortable of sticking with the patterns and methods they generally use and not learning new ones but still trying and learning new tips and techniques after a while is equally important. Well how knowledgeable are you about JavaScript design patterns? Have you used SQL joins? These are not new techniques but if you do not know how to use them then you definitely are new to them. Set aside some time to also focus on existing languages and software, such as chat live for ecommerce sites or the QR code.

3. Use better programming practices



Each time a better programming language should be used in order to make your web designs the best. The better techniques use the better will be your web design.

4. Correct usage of background color and texts




When planning a combination for your web design you should be very careful about choosing color. Your colors should be such which easily attract your target audience. I would recommend light background with dark text, as the reverse of this decrease the readability 10% or less.

5. Creating seamless background

HTML allows you to embellish web pages in many ways. One of these is, putting a background image using background attribute. Employing an odd or non-related image is absolutely the wrong trick. The reason is that browsers tile the background image. You can either make a very large background image, something like 1000 X 800 pixels or use a smaller seamless image. And the only reason I emphasized on seamless background image is that the FILE SIZE.

6. Choosing foreground colors for web pages and web sites

The basic technique is contrasting background and foreground color. Remember, it’s easy for your visitors to press the back button on their browsers. The trick is use a contrasting color combination for the page background and the foreground text.

7. Create site map for your web site



A site map is one page or sometimes a set of pages, which lists all, or most of the pages on a web site. If your web site is small, consisting of only a few pages, the tip for you is you can have links to all these pages from one page and voila – the site map is ready.

8. Good website navigation



The important tip to remember by all is designed for visitors NOT for yourself or your boss. And that people come to your website for information and so site navigation is very important.

9. Prevent caching of web pages by browsers

When visitor view your page it is stored by their system inside the cache directory. When visitor view your pages again, the browser searches the cache, and if it finds the page there, it displays it immediately. This creates problems to for you as web developers when you update a page since the visitors see an older version (that was stored in the cache) of the page. To prevent this from happening, the easy trick is set the expiration date.

10. Usage of Flash

This it the most important thing which a designer needs while designing as without this the designer can not add any animation or video on their website. This is a technique by which you can give your website a cutting edge over other web sites.
There are many more useful tips, tricks and techniques of web designing which I think are beneficial, but I will definitely discuss the rest in my upcoming articles for you all. I think these basic will help my readers in a great way.

 An Amazing Place to visit If you are a web developer... -http://artatm.com/