You are given string s. Let's call word any largest sequence of consecutive symbols without symbols ',' (comma) and ';' (semicolon). For example, there are four words in string "aba,123;1a;0": "aba", "123", "1a", "0". A word can be empty: for example, the string s=";;" contains three empty words separated by ';'.
You should find all words in the given string that are nonnegative INTEGER numbers without leading zeroes and build by them new string a. String a should contain all words that are numbers separating them by ',' (the order of numbers should remain the same as in the string s). By all other words you should build string b in the same way (the order of numbers should remain the same as in the string s).
Here strings "101", "0" are INTEGER numbers, but "01" and "1.0" are not.
For example, for the string aba,123;1a;0 the string a would be equal to "123,0" and string b would be equal to "aba,1a".
Input
The only line of input contains the string s (1 ≤ |s| ≤ 105). The string contains only symbols '.' (ASCII 46), ',' (ASCII 44), ';' (ASCII 59), digits, lowercase and uppercase latin letters.
Output
Print the string a to the first line and string b to the second line. Each string should be surrounded by quotes (ASCII 34).
If there are no words that are numbers print dash (ASCII 45) on the first line. If all words are numbers print dash on the second line.
Examples
Input
aba,123;1a;0
Output
"123,0"
"aba,1a"
Input
1;;01,a0,
Output
"1"
",01,a0,"
Input
1
Output
"1"
Input
a
Output
"a"
Note
In the second example the string s contains five words: "1", "", "01", "a0", "".
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
We need to parse the given string s. It consists of words separated by commas (',') and semicolons (';'). Words can be empty. We need to classify each word: it's a nonnegative integer number without leading zeroes (except for "0" itself) and containing only digits? Actually the definition: "nonnegative INTEGER numbers without leading zeroes". So a word is a number if:
It is composed solely of digits (0-9).
It does not have leading zeroes: the first character cannot be '0' unless the word is exactly "0".
It can be empty? Empty word is not a number. So empty words go to b.
Thus for each word, we determine if it's a number. We then build two strings:
a: all number words joined by commas (','), preserving order.
b: all other words (non-number) joined by commas (','), preserving order.
If there are no number words, print '-' (dash) for a.
If all words are numbers (i.e., b would be empty), print '-' for b.
But careful: the string a and b should be printed within quotes. So output like: "123,0" or "-". But note that dash is without quotes? The problem statement: "Print the string a to the first line and string b to the second line. Each string should be surrounded by quotes (ASCII 34). If there are no words that are numbers print dash (ASCII 45) on the first line. If all words are numbers print dash on the second line." So the dash is printed without quotes. Actually reading: "If there are no words that are numbers print dash (ASCII 45) on the first line. If all words are numbers print dash on the second line." That suggests that we print exactly "-" (without quotes) on the respective line. Meanwhile, when printing a string, we surround it with quotes. So for a non-empty a (i.e., there is at least one number), we output a quoted string, e.g., "123,0". For a empty a, we output "-". Same for b: if b has at least one word (non-number), output quoted string; else output "-". But careful: b could be non-empty but consist only of empty words? Actually b includes all non-number words, which includes empty words. So if there are empty words, they will be included in b. Since they are empty, when we join them with commas, what does that produce? For example, s = ";;" yields words: "", "", "" (three empty words). They are not numbers because empty. So b will be three empty words joined by commas: ",,". That's a string of two commas. So we output that quoted: ",,". In second example, s="1;;01,a0," yields words: "1", "", "01", "a0", "" (five words). Numbers: only "1" is a number (because "01" has leading zero, so not number; empty not numbers; "a0" has letter). So a: "1". b: "", "01", "a0", "" joined by commas: ",01,a0,". That's exactly the output: "1" and ",01,a0,". Yes.
Thus we need to handle empty words correctly: they are just empty strings. They are considered non-number and added to b. In joining, we will have consecutive commas representing empty words. Good.
Edge Cases:
s length up to 1e5, so we can process in O(n).
Implementation:
We'll iterate through s, splitting by ',' and ';'. Keep a list of words. Since words can be empty, we need to detect when we encounter a delimiter. We'll parse manually:
Initialize start = 0, i = 0. While i < len(s):
if s[i] in [',', ';']:
word = s[start:i] (may be empty if start == i)
process word
start = i+1
i++
After loop, process last word: s[start:len(s)] (even if empty when s ends with delimiter? Actually if s ends with delimiter, then after last delimiter start will be len(s) and word becomes ""). So we need to process it.
But careful: If s ends without delimiter, then the last word is from start to end.
Alternative: Use splitting with re but need to handle empty words. Python's split with multiple delimiters: we could replace ; with , and then split? Actually we can use re.split() with pattern [,;] but that will not keep empty strings if consecutive delimiters? re.split(pattern, s) with capture group? By default, re.split will not include empty strings if the pattern matches at start or end? Actually re.split(r'[,;]', s) will return list of strings. If there are consecutive delimiters, empty strings appear: e.g., re.split(r'[,;]', "1;;2") returns ['1', '', '2']. For trailing delimiter: re.split(r'[,;]', "1;") returns ['1', '']? Let's test: In Python, re.split(r'[,;]', "1;") yields ['1', '']? Actually yes: it will return ['1', '']. So that works. However, if the string starts with delimiter, e.g., ";1", re.split yields ['', '1']? Actually yes: leading empty string is included. So re.split with pattern that doesn't capture, works as needed? But we have to be careful: If the pattern matches at the beginning, the result will start with an empty string. So we get the correct list of words, including empties. However, the pattern might be compiled each time; but it's fine for 1e5 length. Simpler: just iterate manually.
We'll collect two lists: nums and others.
For each word, we check if it's a number:
def is_number(word):
if word == "": return False
# check all digits
if not word.isdigit(): return False
# check leading zero: if len > 1 and word[0] == '0' -> False
if len(word) > 1 and word[0] == '0': return False
return True
But careful: "0" passes because len == 1, digit, no leading zero. Good.
Now join:
if nums: a_str = ",".join(nums) and output f'"{a_str}"'
else: output "-"
if others: b_str = ",".join(others) and output f'"{b_str}"'
else: output "-"
But note: The joining process: for others, there may be empty strings. Python's join will put the delimiter between each element. If an element is empty, it's fine. For example, ["", "01", "a0", ""] -> join with ',' yields ",01,a0,". Good.
Edge Cases:
s may have only delimiters: e.g., s = ",;" . Words: "", "", ""? Let's parse: s = "," -> words: "", ""? Actually one comma: split yields two words: before comma (empty) and after comma (empty). So words: ["", ""]. Both are non-number, so b = "," (since join of ["", ""] -> ","). a: none -> "-". Output first line: "-", second: '","'? Actually that would be quote comma quote? It would be ",". That seems correct.
But the problem statement says: "the string s contains three empty words separated by ';'." So for s=";;", words: "", "", "" (three empty). So that's consistent.
s = "123,abc,;0" -> words: "123","abc","", "0". nums=["123","0"], others=["abc",""] -> b="abc,". Wait join others: ["abc",""] -> "abc,". So output: "123,0" and "abc,". That's correct.
Now potential pitfalls:
The delimiter characters: ',' and ';'. We need to treat them equally.
Words may contain letters, digits, dots, but numbers only if all digits and no leading zero.
Dots: e.g., "1.0" is not number, because contains '.' not digit. So our is_digit check fails.
Large input: using s.split with multiple delimiters might be less efficient? But manual loop is fine.
We'll implement with a simple for-loop.
Pseudo-code:
def solve():
import sys
s = sys.stdin.readline().strip()
# Because s may contain newline at end, but we consider entire line except newline? Input line contains the string s, possibly with trailing newline. s may contain spaces? The problem says only those symbols: '.' , ',' , ';' , digits, letters. No spaces. So strip newline.
words = []
start = 0
n = len(s)
for i in range(n+1): # we need to include i==n to capture last word
if i == n or s[i] in ',;':
word = s[start:i]
words.append(word)
start = i+1
# Now process words
nums = []
others = []
for w in words:
if w.isdigit() and (len(w) == 1 or w[0] != '0'):
nums.append(w)
else:
others.append(w)
# Build output
if nums:
sys.stdout.write('"' + ','.join(nums) + '"\n')
else:
sys.stdout.write('-\n')
if others:
sys.stdout.write('"' + ','.join(others) + '"\n')
else:
sys.stdout.write('-\n')
But note: .isdigit() works for Unicode digits, but ASCII digits are fine. For empty string, .isdigit() returns False.
But one nuance: The input string can contain '.' characters. .isdigit() will return False for dot. So okay.
Testing with s = "1.23". isdigit false.
Now consider leading zeros: Check (len(w)==1 or w[0]!='0') ensures "0" passes, "00" fails.
Edge: The word could be something like "0a"? isdigit false because a.
Now potential complexity: building the output strings. Join will create strings of length O(n). Acceptable.
But careful: The words list size can be up to len(s)+1 in worst case (if all delimiters). So memory O(n). That's okay for 1e5.
Alternatively we could process on the fly without storing all words, but storing is fine.
Now ensure that we correctly parse consecutive delimiters: The loop condition: i from 0 to n inclusive. When i hits delimiter or end, we extract from start to i (exclusive). For consecutive delimiters, start becomes i+1, and at the next delimiter, start may equal i, so word = ''.
Testing with s = ";;": n=2. i=0: s[0] is ';', word = s[0:0]='', append. start=1. i=1: s[1] is ';', word = s[1:1]='', append. start=2. i=2: i==n, word = s[2:2]='', append. So three empty words. Good.
Now output: b = ",,"? Actually joining three empty strings with comma: "", "", "" => join: ",," (two commas). So b_str = ",,". Output quoted. Good.
Now one nuance: The problem statement: "Each string should be surrounded by quotes (ASCII 34)." So we need to output exactly quotes around the string. In Python we can do: print(f'"{a_str}"') or write with escaped. Works.
Now check if there might be any tricky case with the dash: If nums empty, print dash without quotes. That's exactly what we do: sys.stdout.write('-\n').
Now verify with second example: Input: 1;;01,a0, -> Our parsing yields words: "1", "", "01", "a0", ""? Let's simulate:
s = "1;;01,a0,". Actually trailing comma at end: s ends with ','. So words: i=0: s[0]='1' not delimiter. i=1: s[1]=';', word = s[0:1]="1", append. start=2. i=2: s[2]=';', word = s[2:2]? Actually start=2, i=2 => word = "" (empty). Append. start=3. i=3: s[3]='0'? Actually s = "1;;01,a0,". Let's index: characters: 0:'1', 1:';', 2:';', 3:'0', 4:'1', 5:',', 6:'a', 7:'0', 8:','. So after i=2: start=3. i=3: not delimiter. i=4: not delimiter. i=5: s[5]=',' delimiter => word = s[3:5]="01", append. start=6. i=6: s[6]='a' not delimiter. i=7: '0' not delimiter. i=8: ',' delimiter => word = s[6:8]="a0", append. start=9. i=9: i==n? n=9? Actually length 9? Let's count: positions 0-8 inclusive => n=9. i=9: i==n => word = s[9:9]="" (since start=9), append. So words: "1", "", "01", "a0", "". Good.
So b = ",".join(["", "01", "a0", ""]) = "" + "," + "01" + "," + "a0" + "," + ""? Actually join of list of length 4: elements: e1="", e2="01", e3="a0", e4="" => result: "" + (if i>0) add delimiter before each element? Python join works: delimiter.join(list) concatenates strings with delimiter between each pair. So result = e1 + ',' + e2 + ',' + e3 + ',' + e4 = "" + "," + "01" + "," + "a0" + "," + "" = ",01,a0,". Good.
Now ensure a and b strings are built correctly.
Edge: What if there is a word that is a number but very long? Up to 1e5 maybe. isdigit will check O(length). That's fine.
Now note: The problem includes '.' as a symbol that can appear in the string. But numbers must be integer numbers without leading zeroes, so '.' cannot appear in a number. So it's fine.
Potential pitfall: Some words may be just "0" and that is a number. Good.
Now test with s = ""? Not possible because 1 ≤ |s| ≤ 10^5. So s non-empty.
Now also test with s = ",". Words: "", "". nums empty, others: ["",""] => b_str = join(["",""]) = ",". So output: - and ","? But is that correct? According to the problem: "the string s contains three empty words separated by ';'" for ";;". For ",", there are two empty words. So b = ",". Output: first line: -, second line: ",". That seems plausible.
Now check output format: Each line should be exactly as specified: quote string quote, or dash. No extra spaces.
Now confirm that the join of empty list yields empty string, but we never call join on empty list because we check if list is non-empty.
Now let's think about performance: We can avoid storing all words twice by processing on the fly: we can iterate and extract words and immediately classify, then store in two lists. However, we need to output in order, so we need to store numbers and others in two separate lists. That's fine.
Now consider memory: worst-case each word could be large, but total characters across all words is at most n plus number of delimiters. Storing strings as slices of original string could be done by storing indices. However, Python strings are immutable, and slicing creates new string. If we store many small strings, that's okay. But worst-case, if there are many delimiters, we might have many empty strings (like n delimiters produce n+1 empty words). Storing that many empty strings? In Python, each empty string is a separate object? Actually, empty strings are interned? Possibly all refer to the same singleton. But anyway, storing up to 1e5 entries in a list is fine.
But we could avoid storing empty strings by storing them as something else? But they must be included in b or a? empty strings are non-numbers, so they go to b. So we need to represent them. If we store as empty string '', that's fine.
So our approach works.
Now confirm that we don't forget to treat '.' as part of numbers? Actually numbers are integer, no decimal point allowed.
Now also note that the string s can contain uppercase and lowercase letters. So .isalpha() not used.
Now final code.
But caution: Input may contain newline char. We use .rstrip('\n') or .strip()? But if s contains leading/trailing spaces? No spaces. So .strip() is safe but it removes whitespace which we shouldn't have. However, there is no whitespace. But we must not inadvertently remove characters that are part of s. Since s may contain only those symbols and letters, no whitespace. So using .strip() to remove newline is fine. Better: s = sys.stdin.readline().rstrip('\n') to only remove newline, preserving any other whitespace (though none). But .strip() would remove tabs/spaces if any, but not expected. Safer to use .rstrip('\n').
Now final code:
import sys
def main():
s = sys.stdin.readline().rstrip('\n')
n = len(s)
nums = []
others = []
start = 0
for i in range(n+1):
if i == n or s[i] in ',;':
word = s[start:i]
# classify
if word.isdigit() and (len(word) == 1 or word[0] != '0'):
nums.append(word)
else:
others.append(word)
start = i+1
# output
if nums:
sys.stdout.write('"' + ','.join(nums) + '"\n')
else:
sys.stdout.write('-\n')
if others:
sys.stdout.write('"' + ','.join(others) + '"\n')
else:
sys.stdout.write('-\n')
if name == "main":
main()
Check: For s = "1", loop: i=0 not delimiter, i=1 i==n => word = s[0:1]="1". Classify: isdigit, len>1? len=1 so pass, nums append "1". start=2 (but i+1=2). Then done. Others empty, so output "1" and "-". Good.
Now test with s = ";;". n=2. i=0: s[0]==';' -> word = s[0:0]='', classify: '' is not isdigit, else false, so others append ''; start=1. i=1: s[1]==';' -> word = s[1:1]='', others append ''; start=2. i=2: i==n -> word = s[2:2]='', others append ''; start=3. nums empty, others list has three empty strings. Then join others: ','.join(['','','']) => ',,'. Output: '-' and '",,"'? Actually code writes '"' + ',,' + '"' -> '",,"'. That matches expectation? For s=";;" what would be expected? Problem didn't give an example, but logically a: dash, b: quoted ",,". Acceptable.
Now check with s="0". nums=["0"], others empty. Output: '"0"' and '-'. Good.
Now consider large input: 1e5 length, loop O(n) fine.
Now note: The isdigit() method returns True for digits like Unicode digits, but problem only uses ASCII digits, so fine.
One nuance: Are we supposed to treat empty word as a number? No because empty string is not a number.
Now confirm edge case: Word like "000"? isdigit true, len>1, first char '0' => not number. So it's non-number.
Now check with s="0,000" -> words: "0","000". nums: ["0"], others: ["000"] -> a: "0", b: "000". Good.
Now check with s="0;000" -> same.
Now check with s = "123abc" -> since no delimiters, one word "123abc". isdigit false -> a: '-', b: "123abc". Good.
Now check with s = "123" -> number, a: "123", b: '-'.
Now check with s = "01" -> non-number.
Now all good.
Potential pitfalls: The input could be very large and the join may be memory heavy but it's fine.
Now we might want to avoid building the entire others string if it's huge? But we need to output it anyway, so we need to produce the string. join is efficient.
Now code is ready.
But consider the scenario: There are no words at all? Not possible because s length >=1, so at least one word.
Now final answer.
Now check output formatting: In example 2, the output for b is ",01,a0," exactly with quotes. Our code will output " + ',01,a0,' + " -> ",01,a0,". That's correct.
Now also verify that if b is empty string? Could b be empty string? That would happen if others list is empty, which means all words are numbers. Then we output dash. So we won't output empty quoted string.
Now one more nuance: The problem states: "If all words are numbers print dash on the second line." So when others list is empty, output dash.
Thus code correct.
Now we can produce final answer.
But note: In the second example, b includes a trailing comma: ",01,a0,". In our join of ["", "01", "a0", ""] it yields exactly that. Good.
Now think about the case where s ends with a delimiter: we correctly capture the last empty word. Good.
Now final answer.
We should maybe use sys.stdout.write for faster output.
Now ensure code uses no extraneous prints.
Now ready.
Now we could also implement using split with regex for simplicity but manual loop is clearer.
Now final answer code. ```python
import sys
def main() -> None:
s = sys.stdin.readline().rstrip('\n')
n = len(s)
nums = []
others = []
start = 0
code
for i in range(n + 1):
if i == n or s[i] in ',;':
word = s[start:i]
# check if word is a non‑negative integer without leading zeros
if word.isdigit() and (len(word) == 1 or word[0] != '0'):
nums.append(word)
else:
others.append(word)
start = i + 1
# output a
if nums:
sys.stdout.write('"' + ','.join(nums) + '"\n')
else:
sys.stdout.write('-\n')
# output b
if others:
sys.stdout.write('"' + ','.join(others) + '"\n')
else:
sys.stdout.write('-\n')