A numeric string, s, is beautiful if it can be split into a sequence of two or more positive integers, a[1], a[2], …, a[n],
satisfying the following conditions:
Perform q queries where each query consists of some integer string s. For each query, print whether or not the string is beautiful on a new line. If it is beautiful, print YES x, where x is the first number of the increasing sequence. If there are multiple such values of x, choose the smallest. Otherwise, print NO.
一個數字字串 s 可以拆分成兩個或多個正整數的陣列 a[1], a[2], ..., a[n], 滿足以下條件: 執行 q 個查詢,其中每個查詢由一些整數字串 s 組成。 對於每個查詢,在新的一行中印出字符串是否美觀。 如果很漂亮,則打印 YES x,其中 x 是遞增序列的第一個數字。 如果有多個這樣的 x 值,則選擇最小的。 否則,打印NO。
Example 1:
string = 7 1234 91011 99100 101103 010203 13 1
solution:
判斷給予的字串 s 是否可以由一個連續的數字增加所組成。
程式從 1 開始,逐個遞增數字,並將遞增後的數字串接到一起,
直到串接後的數字與 s 相等或超過 s 的長度。
如果找到了這樣的數字,就返回 “YES” 加上這個數字的起始部分;否則,返回 “NO”。
Code 1: BigO(n)
'use strict'; process.stdin.resume(); process.stdin.setEncoding('utf-8'); let inputString = ''; let currentLine = 0; process.stdin.on('data', function(inputStdin) { inputString += inputStdin; }); process.stdin.on('end', function() { inputString = inputString.split('\n'); main(); }); function readLine() { return inputString[currentLine++]; } /* * Complete the 'separateNumbers' function below. * * The function accepts STRING s as parameter. */ function separateNumbers(s) { // Write your code here for (let i = 1; i < s.length; i++) { // use BigInt for test cases with large numbers let curr = BigInt(s.slice(0, i)); let temp = curr.toString(); while (temp.length < s.length) { curr++; temp += curr.toString(); } if (temp === s) { return console.log(`YES ${ s.slice(0, i) }`); } } return console.log("NO"); } function main() { const q = parseInt(readLine().trim(), 10); for (let qItr = 0; qItr < q; qItr++) { const s = readLine(); separateNumbers(s); } }
FlowChart:
Example 1
s = 7 1234 91011 99100 101103 010203 13 1 1234 YES 1 91011 YES 9 99100 YES 99 101103 NO 010203 NO 13 NO 1 NO