Knowee
Questions
Features
Study Tools

You are given a collection of N non-empty strings, denoted by S 1 , S 2 , … , S n . Then you are given N - 1 operations which you execute in the order they are given. The i t h operation is has the following format: ‘ a b ’ ( 1 -based indexing, without the quotes), which means that you have to make the following changes: S a = S a + S b , i.e. concatenate a t h string and b t h string and store the result in a t h string, S b = "", i.e. make the b t h string empty, after doing the previous step. You are ensured that after the i t h operation, there will be no future operation that will be accessing S b . Given these operations to join strings, print the last string that will remain at the end of this process. Input The first line contains an integer N ( 1 ≤ N ≤ 10 5 ) denoting the number of strings given. Each of the next N lines contains a string denoting the S i . All the characters in the string S i are lowercase alphabets from ‘a’ to ‘z’. The total number of characters over all the strings is at most 10 6 , i.e ∑ i = 1 N | S i | ≤ 10 6 , where | S i | denotes the length of the i t h string. After these N strings, each of the next N - 1 lines contain two integers a and b , such that a ≠ b and 1 ≤ a , b ≤ N denoting the i t h operation. Output Print the last string which remains at the end of the N - 1 operations. Warning The I/O files are large. Please use fast I/O methods. can you write a java solution, and pick the one that is the most efficient time wise

Question

You are given a collection of N non-empty strings, denoted by S 1 , S 2 , … , S n . Then you are given N

1 operations which you execute in the order they are given. The i t h operation is has the following format: ‘ a

b ’ ( 1 -based indexing, without the quotes), which means that you have to make the following changes:

S a

S a + S b , i.e. concatenate a t h string and b t h string and store the result in a t h string,

S b = "", i.e. make the b t h string empty, after doing the previous step.

You are ensured that after the i t h operation, there will be no future operation that will be accessing S b . Given these operations to join strings, print the last string that will remain at the end of this process.

Input

The first line contains an integer N ( 1 ≤ N ≤ 10 5 ) denoting the number of strings given. Each of the next N lines contains a string denoting the S i . All the characters in the string S i are lowercase alphabets from ‘a’ to ‘z’. The total number of characters over all the strings is at most 10 6 , i.e ∑ i

1 N | S i | ≤ 10 6 , where | S i | denotes the length of the i t h string. After these N strings, each of the next N

1 lines contain two integers a and b , such that a ≠ b and 1 ≤ a , b ≤ N denoting the i t h operation.

Output

Print the last string which remains at the end of the N

1 operations.

Warning

The I/O files are large. Please use fast I/O methods.

can you write a java solution, and pick the one that is the most efficient time wise

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

Sure, here is a Java solution using the concept of Disjoint Set Union (DSU) to solve this problem. The DSU data structure allows us to merge different sets together and perform queries on them in nearly constant time.

import java.util.*;
import java.io.*;

public class Main {
    static int parent[];
    static StringBuilder str[];

    static int find(int a) {
        if (parent[a] == a)
            return a;
        return parent[a] = find(parent[a]);
    }

    static void union(int a, int b) {
        int parent_a = find(a);
        int parent_b = find(b);
        if (parent_a != parent_b) {
            parent[parent_b] = parent_a;
            str[parent_a].append(str[parent_b]);
        }
    }

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());
        parent = new int[n + 1];
        str = new StringBuilder[n + 1];
        for (int i = 1; i <= n; i++) {
            parent[i] = i;
            str[i] = new StringBuilder(br.readLine());
        }
        for (int i = 1; i < n; i++) {
            StringTokenizer st = new StringTokenizer(br.readLine());
            int a = Integer.parseInt(st.nextToken());
            int b = Integer.parseInt(st.nextToken());
            union(a, b);
        }
        System.out.println(str[find(1)]);
    }
}

In this solution, we maintain an array parent[] to keep track of the parent of each string, and an array str[] of StringBuilder objects to store the strings. The find() function is used to find the parent of a string, and the union() function is used to merge two strings together.

The time complexity of this solution is O(N), where N is the number of strings. This is because we perform a constant amount of work for each string. The space complexity is also O(N), as we need to store the parent and string for each string.

This problem has been solved

Similar Questions

You are given a collection of N non-empty strings, denoted by S 1 , S 2 , … , S n . Then you are given N - 1 operations which you execute in the order they are given. The i t h operation is has the following format: ‘ a b ’ ( 1 -based indexing, without the quotes), which means that you have to make the following changes: S a = S a + S b , i.e. concatenate a t h string and b t h string and store the result in a t h string, S b = "", i.e. make the b t h string empty, after doing the previous step. You are ensured that after the i t h operation, there will be no future operation that will be accessing S b . Given these operations to join strings, print the last string that will remain at the end of this process. Input The first line contains an integer N ( 1 ≤ N ≤ 10 5 ) denoting the number of strings given. Each of the next N lines contains a string denoting the S i . All the characters in the string S i are lowercase alphabets from ‘a’ to ‘z’. The total number of characters over all the strings is at most 10 6 , i.e ∑ i = 1 N | S i | ≤ 10 6 , where | S i | denotes the length of the i t h string. After these N strings, each of the next N - 1 lines contain two integers a and b , such that a ≠ b and 1 ≤ a , b ≤ N denoting the i t h operation. Output Print the last string which remains at the end of the N - 1 operations. Warning The I/O files are large. Please use fast I/O methods. can you write a java solution, and pick the one that is the most efficient time wise

Johnwick has a string s1s2...sn and a character c , he only loves character c so he wants to change all character of his string into c using the minimum number of operations. In one operation he can choose a number x (1≤x≤n) and for every position i, where i is not divisible by x, replace si with c. Note - You have to complete the function solve and return - One vector of size m (number of operation you needed) of element which you have taken as x in the same order. if no operation needed then just return empty vector. Input Format : First line of input will contain one integer t - number of test cases(1 <= t <= 100). The first line of each test case contains the integer n (3 ≤ n ≤ 3⋅10^4) and a lowercase Latin letter c — the length of the string s and the character the resulting string should consist of. The second line of each test case contains a string s of lowercase Latin letters — the initial string. It is guaranteed that the sum of n over all test cases does not exceed 3⋅10^4. Output Format : You will receive 1 your answer is ok otherwise -1. Sample Input : 3 4 a aaaa 4 a baaa 4 b bzyx Sample Output: 1 1 1 give java code

Given 2 binary strings A and B of the same length, find the minimum number of operations needed to change string A to B. The operations are defined as follows:AND Operation:Choose a pair of indices i and j such that i != j and perform the following sequence of operations.result = Ai & AjAi = result & AiAj = result & AjOR Operation:Choose a pair of indices i and j such that i != j and perform the following sequence of operations.result = Ai | AjAi = result | AiAj = result | AjXOR Operation:Choose a pair of indices i and j such that i != j and perform the following sequence of operations.result = Ai ^ AjAi = result ^ AiAj = result ^ AjInput FormatThe first line of input contains T - the number of test cases. It's followed by 2T lines, the first line contains string A and the second line contains string B.Output FormatFor each test case, if conversion is possible, print "YES" followed by the minimum number of operations required to convert string A to string B, otherwise print "NO", separated by a new line.Constraints1 <= T <= 10001 <= len(A)==len(B) <= 103ExampleInput210101010101011OutputYES 2YES 1ExplanationTest-Case 1We can choose index-1 and index-2 of string A and perform the XOR operation. The resultant string will look like 110. After that, we can choose index-0 and index-2 of the new string A and perform the AND operation. The resultant string will look like 010.

(i) Without using string slicing operations, create a function which takes in a string, reverse(string), and returns a new string in the reverse order.

program must accept two string values S1 (may contain space) and S2 (one word) as the input. The words in S1 are sorted lexicographically and separated by exactly one space. The program must insert the the string S2 in S1 such that all the words in the modified S1 are also in sorted order.Note: The string values contain only lower case alphabetsBoundary Condition(s):1 <= Length of S1 <= 10001 <= Length of S2 <= 100Input Format:The first line contains S1.The second line contains S2.Output Format:The first line contains the modified S1.Example Input/Output 1:Input:abacus ball dog hat mind zebrainkOutput:abacus ball dog hat ink mind zebraExample Input/Output 2:Input:captain celbrate cricketcrackerOutput:captain celbrate cracker cricket

1/1

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.