Showing posts with label Concepts on C and data structures. Show all posts
Showing posts with label Concepts on C and data structures. Show all posts

Monday, 11 June 2012

C-Programming Challenge



Question 1: Time – 2Hr
Given a list of words, L, that are all the same length, and a string, S, find the starting position of the substring of S that is a concatenation of each word in L exactly once and without any intervening characters.  This substring will occur exactly once in S.
Example:
L: "fooo", "barr", "wing", "ding", "wing"
S: "lingmindraboofooowingdingbarrwingmonkeypoundcake"
                 fooowingdingbarrwing
Answer: 13

L: "mon", "key"
S: "monkey
    monkey
Answer: 0

L: "a", "b", "c", "d", "e"
S: "abcdfecdba"
         ecdba
Answer: 5

The first line of input specifies L, it will contain between 1 and 100 space-separated words, each between 1 and 10 characters long.
The second line of input specifies S, the line will contain a single word up to 1 million characters long.
The characters in both L and S should be treated as case-sensitive.


Question 2: Time 45min

There are K pegs. Each peg can hold discs in decreasing order of radius when looked from bottom to top of the peg. There are N discs which have radius 1 to N; Given the initial configuration of the pegs and the final configuration of the pegs, output the moves required to transform from the initial to final configuration. You are required to do the transformations in minimal number of moves.
A move consists of picking the topmost disc of any one of the pegs and placing it on top of any other peg.
At anypoint of time, the decreasing radius property of all the pegs must be maintained.

Constraints:
1<= N<=8
3<= K<=5

Input Format:

N K
2nd line contains N integers.
Each integer in the second line is in the range 1 to K where the i-th integer denotes the peg to which disc of radius i is present in the initial configuration.

3rd line denotes the final configuration in a format similar to the initial configuration.

Output Format:

The first line contains M - The minimal number of moves required to complete the transformation.
The following M lines describe a move, by a peg number to pick from and a peg number to place on.
If there are more than one solutions, it's sufficient to output any one of them. You can assume, there is always a solution with less than 7 moves and the initial confirguration will not be same as the final one.

Sample Input #00:
2 3
1 1
2 2

Sample Output #00:
3
1 3
1 2
3 2

Sample Input #01:
6 4
4 2 4 3 1 1
1 1 1 1 1 1

Sample Output #01:
5
3 1
4 3
4 1
2 1
3 1

Saturday, 19 May 2012

Structures in C


1. Structure is capable of holding heterogenous data.i.e data of different types are grouped together under single name called structure.

2. Structure tmplete doesnot reserver any space in memory for members..Space reserved only when structure variable is declared.

Struct nagendra {
int marks;
char subject[];
  } //here structure variable is not declared...so no space is reserved in memory for this structure.


Struct nagendra {
int marks;
char subject[];
  }variable1; //variable is declared so memory will be reserved for structure


3. Members names inside structure should be different from each other but it can be similar to ny other variables outside the stucture

4. Structure variables can be defines in 2 ways

a. struct student{
char name[22];
int rollno;
float marks; //with structure definition
     }stu1,stu2,stu3;


b. struct student{

char name[22];
int rollno;
float marks; //using stucture Tag
     };
struct student stu1,stu2;
struct student stu3;

5. Compiler will reserve enough space for each variables(stu1,stu2,stu3) that is sufficient to hold all the members.For example each variable in above example of type struct student will occupy (22+4+4)bytes.

6. Initializing of members inside the structure is not possible.

Ex: struct student{

char[22]="nagendra"; //Invalid
int marks=10; //Invalid
     }

7. In structures leaving variable initilizer blank,then by default it initializes to values of 0..Lets represent it as follows.

struct student stu1={"nagendra"};  this is equivalent to struct student stu1={"nagendra",0,0};

8. Members of structures are stored in consecutive memory locations.







Tuesday, 1 May 2012

Declaration Definition and Initialization | FORWARD REFERENCE

  • During declaration space is not reserved for the variable program memory, its conveys the compiler that the variable is appeared in the program.Technically this way of conveying to the compiler is known as "FORWARD REFERENCE".
  • By declaring we tell the datatype of variable and during definition the value is initialized.
  • Function prototype contains function's name, argument types and return type.Its just a declaration of variable that doesn't contain any function body.
  • If both global variable and local variable arives in the function at a time then priority is given to the local variable.
  • Suppose the automatic array is partially initialized then remaining elements are initialized to zero.
  • There is a similarity in Array and Structures with respect to initialization i.e., if partially initialized both assigns 0 to rest of elements.

Thursday, 26 April 2012

FORWARD REFERENCE In C | Hinting In C

During Declaration space is never reserved for the variable or instance in the program's memory; it simply a hint to the compiler that a use of the variable or instance is expected in the program. This hinting is technically called "forward reference"

Example : STANDARD C LIBRARY contains definition of header files whereas #include<stdio.h> is just a declaration.


During declaration we tell about the datatype of Variable used. While definition the value is initialized.
Declaration can done only once in the program whereas Definition can occur many times in the program.

EXAMPLE :        int a;            // declaration

                          int a=421;    // definition