Bob wants to purchase N pens and so has prepared a list of shops which sell the pens. Each shop charges a different price per pen and has limited quantity available in stock. Along with the price of pen, each shop charges fixed delivery fees irrespective of how many pens are bought. Write a program to help Bob get the N pens as cheaply as
possible.
Read the input from STDIN and print the output to STDOUT. Do not write arbitrary strings while reading the input or while printing, as these contribute to the standard output.
The first line of input has two integers: N, the number of pens Bob needs, and S, the number of shops, separated by single white space.
Next S lines have 3 integers, Q, P and D, each separated by single white spaces, where Q is the stock of
fee.
Single line of output has an integer, which is the minimum amount Bob has to spend to purchase N pen
i) 1 <= N <=10000
ii) 1 <= S <= 100
iii) 0 <= Q <= 10000
iv) 0 <= P <= 10000
v) 0 <= D <= 1000000
VI) N <= Q1+Q2+…+Qs
10 2
5 5 50
1000 10 0
100
No of pens Bob needs, N=10
No of shops, S=2
Consider the string str, split it into words based on the whitespaces they are having. Your task is to identify the number of pairs of consecutive characters of each word of str are in lexicographical order from left to right. Repete this process for all the words of the input string and then concatenate the counts of each word of the string in same order and display it as an output.
If there is no character in a word that comes lexicographically after its neighbor to the left, then its count becomes "0'.
Read the input from STDIN and write the output to STDOUT. You should not write arbitrary strings while reading the input standard output.
i. The given string only contains lowercase alphabets.
ii. The input contains only one string and it does not have any special characters in it other than white space.
A single line of input consists of a string str.
A single line of output consists of concatenated counts displayed as a result.
hello world
21
From the given Sample Input1, the given string str is "hello world".
The string str is divided into the substrings “hello” and “world”.
In “hello”, ‘e’ & ‘l’ and ‘l’ & ‘o’ are in lexicographical order. So, now the pair count is 2.
In the word “world”, only ‘o’ and ‘r’ are in lexical order, therefore pair count is 1.
Concatenating both results in 21 so the output is 21.
Overview:
Approach:
Time Complexity:
O(N)
where N
is the length of the string.PseudoCode:
string s;
while(cin>>s){
int n=s.length();
int count=0;
for(int i=1;i < n;i++){
if(s[i] > s[i-1]) count++;
}
cout << count;
}
cout << endl;