The document describes several problems related to strings composed of the letters A, B, and C.
1) The ABC problem asks to generate a string of length N with exactly K pairs of indices where the character at the first index is less than the character at the second index.
2) The ABC Path problem asks to find any path between an initial and target string where each step adds A, B, or C to the end or reverses and adds C.
3) Several examples are given for each problem demonstrating valid and invalid test cases.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
740 views15 pages
Topcoder I
The document describes several problems related to strings composed of the letters A, B, and C.
1) The ABC problem asks to generate a string of length N with exactly K pairs of indices where the character at the first index is less than the character at the second index.
2) The ABC Path problem asks to find any path between an initial and target string where each step adds A, B, or C to the end or reverses and adds C.
3) Several examples are given for each problem demonstrating valid and invalid test cases.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 15
{\\}
1) Aaagmnrs Problem Statement Two phrases are anagrams if they are permutations of each other, ignoring spaces and capitalization. For example, "Aaagmnrs" is an anagram of "anagrams", and "TopCoder" is an anagram of "Drop Cote". Given a String[] phrases, remove each phrase that is an anagram of an earlier phrase, and return the remaining phrases in their original order. Definition Class: Aaagmnrs Method: anagrams Parameters: String[] Returns: String[] Method signature: String[] anagrams(String[] phrases) (be sure your method is public) Constraints - phrases contains between 2 and 50 elements, inclusive. - Each element of phrases contains between 1 and 50 characters, inclusive. - Each element of phrases contains letters ('a'-'z' and 'A'-'Z') and spaces (' ') only. - Each element of phrases contains at least one letter. Examples 0) { "Aaagmnrs", "TopCoder", "anagrams", "Drop Cote" } Returns: { "Aaagmnrs", "TopCoder" } The examples above. 1) { "SnapDragon vs tomek", "savants groped monk", "Adam vents prongs ok" } Returns: { "SnapDragon vs tomek" } 2) { "Radar ghost jilts Kim", "patched hers first", "DEPTH FIRST SEARCH", "DIJKSTRAS ALGORITHM" } Returns: { "Radar ghost jilts Kim", "patched hers first" } 2) AB Problem Statement You are given two ints: N and K. Lun the dog is interested in strings that satisfy the following conditions: The string has exactly N characters, each of which is either 'A' or 'B'. The string s has exactly K pairs (i, j) (0 <= i < j <= N-1) such that s[i] = 'A' and s[j] = 'B'. If there exists a string that satisfies the conditions, find and return any such string. Otherwise, return an empty string. Definition Class: AB
Method: createString Parameters: int, int Returns: String Method signature: String createString(int N, int K) (be sure your method is public) Constraints - N will be between 2 and 50, inclusive. - K will be between 0 and N(N-1)/2, inclusive. Examples 0) 3 2 Returns: "ABB" This string has exactly two pairs (i, j) mentioned in the statement: (0, 1) and (0, 2). 1) 2 0 Returns: "BA" Please note that there are valid test cases with K = 0. 2) 5 8 Returns: "" Five characters is too short for this value of K. 3) 10 12 Returns: "BAABBABAAB" Please note that this is an example of a solution; other valid solutions will also be accepted. 3) Abacus Problem Statement An abacus can be used to do arithmetic. The version that we have has 6 horizontal threads, each with nine beads on it. The beads on each thread are always arranged with just one gap, possibly at one of the ends. However many beads are adjacent and at the right end of the thread is the digit value of the thread. The value on the abacus is read by taking the digits in order from top thread to bottom thread and arranging them from left to right (so the top thread is the one that contains the most significant digit). Create a class Abacus that contains a method add that is given a String[] original and a number val and that returns a String[] showing the abacus after val has been added to the original abacus. Both in original and in the return, the String[] will contain exactly 6 elements representing the 6 threads in order from top thread to bottom thread. Each element will contain a lowercase 'o' to represent each bead and three consecutive hyphens '-' to
indicate the empty part of the thread. Each element will thus contain exactly 12 characters. Definition Class: Abacus Method: add Parameters: String[], int Returns: String[] Method signature: String[] add(String[] original, int val) (be sure your method is public) Constraints - original will contain exactly 6 elements. - Each element of original will contain exactly 12 characters, 9 lowercase 'o's and 3 consecutive '-'s. - val will be between 0 and 999,999 inclusive. - val added to the original abacus will result in a value that can be shown on the abacus. Examples 0) {"ooo---oooooo", "---ooooooooo", "---ooooooooo", "---ooooooooo", "oo---ooooooo", "---ooooooooo"} 5 Returns: {"ooo---oooooo", "---ooooooooo", "---ooooooooo", "---ooooooooo", "o---oooooooo", "ooooo---oooo" } When we add 5 to the original, it is necessary to "carry" 1 to the next thread up. This shows the arithmetic 699979 + 5 = 699984 1) {"ooo---oooooo", "---ooooooooo", "---ooooooooo", "---ooooooooo", "oo---ooooooo", "---ooooooooo"} 21 Returns: {"oo---ooooooo", "ooooooooo---",
"ooooooooo---", "ooooooooo---", "ooooooooo---", "ooooooooo---" } This shows 699979 + 21 = 700000 2) {"ooooooooo---", "---ooooooooo", "ooooooooo---", "---ooooooooo", "oo---ooooooo", "---ooooooooo"} 100000 Returns: {"oooooooo---o", "---ooooooooo", "ooooooooo---", "---ooooooooo", "oo---ooooooo", "---ooooooooo" } 3) {"o---oooooooo", "---ooooooooo", "---ooooooooo", "---ooooooooo", "---ooooooooo", "---ooooooooo" } 1 Returns: {"---ooooooooo", "ooooooooo---", "ooooooooo---", "ooooooooo---", "ooooooooo---", "ooooooooo---" } 4) ABBA Problem Statement One day, Jamie noticed that many English words only use the letters A and B. Examples of such words include "AB" (short for abdominal), "BAA" (the noise a sheep makes), "AA" (a type of lava), and "ABBA" (a Swedish pop sensation). Inspired by this observation, Jamie created a simple game. You are given two Strings: initial and target. The goal of the game is to find a sequence of valid moves that will change initial into target. There are two types of valid moves:
Add the letter A to the end of the string.
Reverse the string and then add the letter B to the end of the string. Return "Possible" (quotes for clarity) if there is a sequence of valid moves that will change initial into target. Otherwise, return "Impossible". Definition Class: ABBA Method: canObtain Parameters: String, String Returns: String Method signature: String canObtain(String initial, String target) (be sure your method is public) Constraints - The length of initial will be between 1 and 999, inclusive. - The length of target will be between 2 and 1000, inclusive. - target will be longer than initial. - Each character in initial and each character in target will be either 'A' or 'B'. Examples 0) "B" "ABBA" Returns: "Possible" Jamie can perform the following moves: Initially, the string is "B". Jamie adds an 'A' to the end of the string. Now the string is "BA". Jamie reverses the string and then adds a 'B' to the end of the string. Now the string is "ABB". Jamie adds an 'A' to the end of the string. Now the string is "ABBA". Since there is a sequence of moves which starts with "B" and creates the string "ABBA", the answer is "Possible". 1) "AB" "ABB" Returns: "Impossible" The only strings of length 3 Jamie can create are "ABA" and "BAB". 2) "BBAB" "ABABABABB" Returns: "Impossible" 3) "BBBBABABBBBBBA" "BBBBABABBABBBBBBABABBBBBBBBABAABBBAA" Returns: "Possible" 4) "A"
"BB" Returns: "Impossible" 5) ABBA Div 1 Problem Statement One day, Jamie noticed that many English words only use the letters A and B. Examples of such words include "AB" (short for abdominal), "BAA" (the noise a sheep makes), "AA" (a type of lava), and "ABBA" (a Swedish pop sensation). Inspired by this observation, Jamie created a simple game. You are given two Strings: initial and target. The goal of the game is to find a sequence of valid moves that will change initial into target. There are two types of valid moves: Add the letter A to the end of the string. Add the letter B to the end of the string and then reverse the entire string. (After the reversal the newly-added B becomes the first character of the string). Return "Possible" (quotes for clarity) if there is a sequence of valid moves that will change initial into target. Otherwise, return "Impossible". Definition Class: ABBADiv1 Method: canObtain Parameters: String, String Returns: String Method signature: String canObtain(String initial, String target) (be sure your method is public) Constraints - The length of initial will be between 1 and 49, inclusive. - The length of target will be between 2 and 50, inclusive. - target will be longer than initial. - Each character in initial and each character in target will be either 'A' or 'B'. Examples 0) "A" "BABA" Returns: "Possible" Jamie can perform the following moves: Initially, the string is "A". Jamie adds a 'B' to the end of the string and then reverses the string. Now the string is "BA". Jamie adds a 'B' to the end of the string and then reverses the string. Now the string is "BAB". Jamie adds an 'A' to the end of the string. Now the string is "BABA". Since there is a sequence of moves which starts with "A" and creates the string "BABA", the answer is "Possible". 1)
"BAAAAABAA" "BAABAAAAAB" Returns: "Possible" Jamie can add a 'B' to the end of the string and then reverse the string. 2) "A" "ABBA" Returns: "Impossible" 3) "AAABBAABB" "BAABAAABAABAABBBAAAAAABBAABBBBBBBABB" Returns: "Possible" 4) "AAABAAABB" "BAABAAABAABAABBBAAAAAABBAABBBBBBBABB" Returns: "Impossible". 6) ABC Problem Statement You are given two ints: N and K. Lun the dog is interested in strings that satisfy the following conditions: The string has exactly N characters, each of which is either 'A', 'B' or 'C'. The string s has exactly K pairs (i, j) (0 <= i < j <= N-1) such that s[i] < s[j]. If there exists a string that satisfies the conditions, find and return any such string. Otherwise, return an empty string. Definition Class: ABC Method: createString Parameters: int, int Returns: String Method signature: String createString(int N, int K) (be sure your method is public) Constraints - N will be between 3 and 30, inclusive. - K will be between 0 and N(N-1)/2, inclusive.
Examples 0) 3 3 Returns: "ABC"
This string has exactly three pairs (i, j) mentioned in the statement: (0,1), (0,2) and (1,2). 1) 3 0 Returns: "CBA" Please note that there are valid test cases with K = 0. 2) 5 10 Returns: "" Five characters is too short for this value of K. 3) 15 36 Returns: "CABBACCBAABCBBB" Please note that this is an example of a solution; other valid solutions will also be accepted. 7) ABC Path Problem Statement You will be given a 2-dimensional grid of letters. Write a method to find the length of the longest path of consecutive letters, starting at 'A'. Paths can step from one letter in the grid to any adjacent letter (horizontally, vertically, or diagonally). For example, in the following grid, there are several paths from 'A' to 'D', but none from 'A' to 'E': { "ABE", "CFG", "BDH", "ABC" } One such path is: AB. C.. .D. ... (spaces are for clarity only) so, for this grid, your method should return 4. Definition Class: ABCPath Method: length Parameters: String[] Returns: int
Method signature: int length(String[] grid)
(be sure your method is public) Notes - The longest path may start at any 'A' character in the input. Constraints - grid will contain between 1 and 50 elements, inclusive. - Each element of grid will be between 1 and 50 characters long, inclusive. - Each element of grid will have the same length. - grid will contain only uppercase letters ('A'-'Z'). Examples 0) { "ABE", "CFG", "BDH", "ABC" } Returns: 4 This is the example from the problem statement. 1) { "A" } Returns: 1 2) { "BCDEFGHIJKLMNOPQRSTUVWXYZ" } Returns: 0 Paths must start with an 'A'. 3) { "C", "D", "B", "A" } Returns: 2
4) { "KCBVNRXSPVEGUEUFCODMOAXZYWEEWNYAAXRBKGACSLKYRVRKIO" , "DIMCZDMFLAKUUEPMPGRKXSUUDFYETKYQGQHNFFEXFPXNYEFYEX", "DMFRPZCBOWGGHYAPRMXKZPYCSLMWVGMINAVRYUHJKBBRONQEXX", "ORGCBHXWMTIKYNLFHYBVHLZFYRPOLLAMBOPMNODWZUBLSQSDZQ", "QQXUAIPSCEXZTTINEOFTJDAOBVLXZJLYOQREADUWWSRSSJXDBV", "PEDHBZOVMFQQDUCOWVXZELSEBAMBRIKBTJSVMLCAABHAQGBWRP", "FUSMGCSCDLYQNIXTSTPJGZKDIAZGHXIOVGAZHYTMIWAIKPMHTJ", "QMUEDLXSREWNSMEWWRAUBFANSTOOJGFECBIROYCQTVEYGWPMTU", "FFATSKGRQJRIQXGAPLTSXELIHXOPUXIDWZHWNYUMXQEOJIAJDH", "LPUTCFHYQIWIYCVOEYHGQGAYRBTRZINKBOJULGYCULRMEOAOFP", "YOBMTVIKVJOSGRLKTBHEJPKVYNLJQEWNWARPRMZLDPTAVFIDTE", "OOBFZFOXIOZFWNIMLKOTFHGKQAXFCRZHPMPKGZIDFNBGMEAXIJ", "VQQFYCNJDQGJPYBVGESDIAJOBOLFPAOVXKPOVODGPFIYGEWITS", "AGVBSRLBUYOULWGFOFFYAAONJTLUWRGTYWDIXDXTMDTUYESDPK", "AAJOYGCBYTMXQSYSPTBWCSVUMNPRGPOEAVVBGMNHBXCVIQQINJ", "SPEDOAHYIDYUJXGLWGVEBGQSNKCURWYDPNXBZCDKVNRVEMRRXC", "DVESXKXPJBPSJFSZTGTWGAGCXINUXTICUCWLIBCVYDYUPBUKTS", "LPOWAPFNDRJLBUZTHYVFHVUIPOMMPUZFYTVUVDQREFKVWBPQFS", "QEASCLDOHJFTWMUODRKVCOTMUJUNNUYXZEPRHYOPUIKNGXYGBF", "XQUPBSNYOXBPTLOYUJIHFUICVQNAWFMZAQZLTXKBPIAKXGBHXX" } Returns: 19 5) { "EDCCBA", "EDCCBA" } Returns: 3 6) { "AMNOPA", "ALEFQR", "KDABGS", "AJCHUT", "AAIWVA", "AZYXAA" } Returns: 26 8) AbsSequence Problem Statement Let's consider an infinite sequence S of non-negative integers defined as follows: S0 = first; S1 = second; Si = |Si-2 - Si-1| for all i >= 2.
You will be given Strings first and second, representing the 0-th and the 1-st elements of the sequence S, and a String[] indices, each element of which represents a non-negative integer without extra leading zeros. Return a String[] containing as many elements as indices, where the i-th element is equal to the indices[i]-th element of the sequence S (index is 0-based). No element of the return should contain extra leading zeros. Definition Class: AbsSequence Method: getElements Parameters: String, String, String[] Returns: String[] Method signature: String[] getElements(String first, String second, String[] indices) (be sure your method is public) Constraints - first will represent an integer between 0 and 10^18, inclusive, with no extra leading zeros. - second will represent an integer between 0 and 10^18, inclusive, with no extra leading zeros. - indices will contain between 1 and 50 elements, inclusive. - Each element of indices will represent an integer between 0 and 10^18, inclusive, with no extra leading zeros. Examples 0) "21" "12" {"0", "1", "2", "3", "4"} Returns: {"21", "12", "9", "3", "6" } Here S0=21 and S1=12. The next three sequence elements are S2 = |21 - 12| = 9, S3 = |12 - 9| = 3 and S4 = |9 - 3| = 6. 1) "0" "0" {"1000000000000000000"} Returns: {"0" } Here we get the sequence consisting of only zeros. 2) "823" "470" {"3","1","31","0","8","29","57","75","8","77"} Returns: {"117", "470", "2", "823", "115", "87", "49", "25", "115", "23" } 3) "710370" "177300" {"5","95","164721","418","3387","710","0","1197","19507","5848"} Returns: {"178470", "108270", "90", "0", "90", "90", "710370", "90", "0", "0" }
9) AcademicJournal Problem Statement In any field of research, there are many journals to which one can submit an article for publication. One criterion that is commonly used to choose between journals is the impact factor, a measure of the importance of a journal and the papers that are published there. The impact factor of a journal is defined as the average number of citations each paper in that journal receives from papers in other journals. Citations from papers in the same journal are not counted in order to prevent its editors from inflating their impact factor by preferentially accepting papers that cite other papers in their journal. Although impact factors are not a fair way to judge the quality of research, they do provide a quantitative method for comparing journals to each other. Write a class AcademicJournal with a method rankByImpact that takes a String[] papers, a collection of published papers from which to calculate impact factors, and returns a String[] with the journal names sorted in decreasing order by impact factor. Each element of papers contains the information for a single paper, and is formatted with the name of the paper's journal (consisting of only uppercase English characters and spaces), followed by a period, followed by zero or more integers specifying the zero-based indices of the papers it cites. The citation indices contain no extra leading zeroes, and are separated from each other and from the period character by single spaces. If there is a tie in the impact factors, the journal with more papers comes first in the return value. If there is still a tie, the journal with the lexicographically earlier name comes first. Definition Class: AcademicJournal Method: rankByImpact Parameters: String[] Returns: String[] Method signature: String[] rankByImpact(String[] papers) (be sure your method is public) Notes - Although it is not supposed to happen, it is possible for two papers to reference each other due to delays in the editing and publishing process. - A sloppy author or editor of a paper might accidentally include multiple citations to another paper. In your calculation of the impact factors, count citations from one paper to another only once.
Constraints - papers will contain between 1 and 50 elements, inclusive. - Each element of papers will contain between 2 and 50 characters, inclusive. - Each element of papers will be formatted as described in the problem statement. - Each index will be between 0 and the number of papers - 1, inclusive. - A paper will not contain a reference to itself.
Examples 0) {"A.", "B. 0", "C. 1 0 3", "C. 2"} Returns: {"A", "B", "C" } The one paper in journal A is cited two times, so A's impact factor is 2/1. The one paper in journal B is cited once, so B's impact factor is 1/1. The two papers in journal C only receive citations from each other. Since citations from a paper in the same journal do not count, C's impact factor is 0/2. 1) {"RESPECTED JOURNAL.", "MEDIOCRE JOURNAL. 0", "LOUSY JOURNAL. 0 1", "RESPECTED JOURNAL.", "MEDIOCRE JOURNAL. 3", "LOUSY JOURNAL. 4 3 3 4", "RESPECTED SPECIFIC JOURNAL.", "MEDIOCRE SPECIFIC JOURNAL. 6", "LOUSY SPECIFIC JOURNAL. 6 7"} Returns: {"RESPECTED JOURNAL", "RESPECTED SPECIFIC JOURNAL", "MEDIOCRE JOURNAL", "MEDIOCRE SPECIFIC JOURNAL", "LOUSY JOURNAL", "LOUSY SPECIFIC JOURNAL" } There is an impact factor tie between the SPECIFIC and non-specific versions of each tier of journal. Since the non-specific ones have more papers, they win the tiebreaker. 2) {"NO CITATIONS.", "COMPLETELY ORIGINAL."} Returns: {"COMPLETELY ORIGINAL", "NO CITATIONS" } If there is a tie in impact factor and number of papers, the journal with the lexicographically earlier name comes first.
3) {"CONTEMPORARY PHYSICS. 5 4 6 8 7 1 9",
"EUROPHYSICS LETTERS. 9",
"J PHYS CHEM REF D. 5 4 6 8 7 1 9", "J PHYS SOC JAPAN. 5 4 6 8 7 1 9", "PHYSICAL REVIEW LETTERS. 5 6 8 7 1 9", "PHYSICS LETTERS B. 6 8 7 1 9", "PHYSICS REPORTS. 8 7 1 9", "PHYSICS TODAY. 1 9", "REP PROGRESS PHYSICS. 7 1 9", "REV MODERN PHYSICS."} Returns: {"REV MODERN PHYSICS", "EUROPHYSICS LETTERS", "PHYSICS TODAY", "REP PROGRESS PHYSICS", "PHYSICS REPORTS", "PHYSICS LETTERS B", "PHYSICAL REVIEW LETTERS", "CONTEMPORARY PHYSICS", "J PHYS CHEM REF D", "J PHYS SOC JAPAN" } 10) AccessChanger Problem Statement You are converting old code for a new compiler version. Each "->" string should be replaced by ".", but this replacement shouldn't be done inside comments. A comment is a string that starts with "//" and terminates at the end of the line. You will be given a String[] program containing the old code. Return a String[] containing the converted version of the code. Definition Class: AccessChanger Method: convert Parameters: String[] Returns: String[] Method signature: String[] convert(String[] program) (be sure your method is public) Constraints - program will have between 1 and 50 elements, inclusive. - Each element of program will contain between 1 and 50 characters, inclusive. - Each character in program will have ASCII value between 32 and 127, inclusive. Examples 0) {"Test* t = new Test();",
"t->a = 1;", "t->b = 2;", "t->go(); // a=1, b=2 --> a=2, b=3"} Returns: {"Test* t = new Test();", "t.a = 1;", "t.b = 2;", "t.go(); // a=1, b=2 --> a=2, b=3" } 1) {"---> // the arrow --->", "---", "> // the parted arrow"} Returns: {"--. // the arrow --->", "---", "> // the parted arrow" } 2) {"->-> // two successive arrows ->->"} Returns: {".. // two successive arrows ->->" }