8223. Prefixes of a substring

 

The prefix of a string S is any string of the form S[1..k], k ≤ size(S) (size(S) is the length of a string). Prefix can be either empty or match the string itself. If prefix is not empty and does not match with the string S, it is called the proper prefix of string S. Print the number of prefixes and all proper prefixes of substring S[i..j] in the increasing order of their lengths.

 

Input. First line contains S, its length is no more than 100. Second line contains two indexes i and j (1 ≤ i ≤ size(S), 1 ≤ j ≤ size(S)).

 

Output. Print in the first line the number of prefixes of substring S[i..j]. In the next lines print all proper prefixes of a given substring. The output format is given in a sample.

 

Sample input

Sample output

abracadabra

2 5

5

b

br

bra

 

 

SOLUTION

strings

 

Algorithm analysis

The number of prefixes is ji + 2. Subtract one from i and j to start the indexation from zero.

Print all own prefixes s[i..k] of the substring s[i..j] (i k < j).

 

Algorithm realization

Declare char arrays.

 

char s[110], temp[110];

 

Read the input data.

 

gets(s);

scanf("%d %d", &i, &j);

i--; j--;

 

Print the number of prefixes.

 

printf("%d\n", j - i + 2);

 

Print all proper prefixes s[i..k] of substring s[i..j]. For this there must be i k < j.

 

for (k = i; k < j; k++)

{

 

Copy s[i..k] to array temp. At the end of the string temp put zero byte.

 

  strncpy(temp, s + i, k - i + 1);

  temp[k - i + 2] = 0;

 

Print the prefix.

 

  puts(temp);

}

 

Java realization

 

import java.util.*;

 

public class Main

{

  public static void main(String[] args)

  {

    Scanner con = new Scanner(System.in);

    String s = con.nextLine();

    int i = con.nextInt();

    int j = con.nextInt();

 

    System.out.println(j - i + 2);

    for(int k = i; k < j; k++)

    {

      String s1 = s.substring(i-1,k);

      System.out.println(s1);

    }

    con.close();

  }

}

 

Python realization

 

s = input()

i, j = map(int,input().split())

print (j - i + 2)

for k in range(i, j):

   print (s[i-1:k])