Advertisement
yoyo106

Untitled

Sep 21st, 2020
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.17 KB | None | 0 0
  1. import java.io.*;
  2. import java.math.*;
  3. import java.security.*;
  4. import java.text.*;
  5. import java.util.*;
  6. import java.util.concurrent.*;
  7. import java.util.function.*;
  8. import java.util.regex.*;
  9. import java.util.stream.*;
  10. import static java.util.stream.Collectors.joining;
  11. import static java.util.stream.Collectors.toList;
  12.  
  13.  
  14.  
  15. class Result {
  16.  
  17. /*
  18. * Complete the 'segments' function below.
  19. *
  20. * The function is expected to return a STRING_ARRAY.
  21. * The function accepts STRING message as parameter.
  22. */
  23.  
  24. public static List<String> segments(String message) {
  25. /*final int limit = 155;
  26. String[] splitted = message.split(" ");
  27. int n = splitted.length;
  28. List<String> ret = new ArrayList<>();
  29.  
  30. for(int i=0; i<n-1; ++i){
  31. String cur = splitted[i];
  32. while((cur+1+splitted[i+1]).length() <= limit && i+1 <n-1){
  33. cur = cur + " " + splitted[i+1];
  34. ++i;
  35. }
  36. ret.add(cur);
  37. }
  38.  
  39. String last = ret.get(ret.size()-1);
  40. if(last.length()+1+splitted[n-1].length() < limit){
  41. ret.set(ret.size()-1,last+ " " + splitted[n-1]);
  42. }else{
  43. ret.add(splitted[n-1]);
  44. }
  45.  
  46. int size = ret.size();
  47. for(int i=0;i<size;++i){
  48. ret.set(i,ret.get(i)+"("+(i+1)+"/"+size+")");
  49. }
  50.  
  51. return ret;*/
  52. List<String> ret = new ArrayList<>();
  53. /*int n = message.length();
  54. int i=0, j = -1;
  55.  
  56. do{
  57. i = j+1;
  58. j = i + 155;
  59. while(j > i && j < n && message.charAt(j--) != ' '){}
  60. if(j<i) break;
  61. if(j>=n){
  62. j = n;
  63. }
  64. ret.add(message.substring(i,j-i));
  65. }while(j >= n-1);
  66.  
  67. return ret;*/
  68.  
  69. /*int i = 0;
  70. int n = message.length();
  71. while(i<n){
  72. int j = Math.max(0,Math.min(i+155,n)-1);
  73. boolean skip = false;
  74. if(j == n-1){
  75. skip = true;
  76. }
  77. else if(j<n && message.charAt(j+1) == ' '){
  78. skip = true;
  79. }
  80. int nextI = Math.max(0,j+1);
  81. if(!skip){
  82. while(i<j && message.charAt(j) != ' '){
  83. //--j;
  84. j= Math.max(0,j-1);
  85. }
  86. nextI = Math.max(0,j+1);
  87. while(i<j && message.charAt(j) == ' '){
  88. //--j;
  89. j= Math.max(0,j-1);
  90. }
  91. }
  92. int start = Math.max(0,i);
  93. int end = Math.max(j-i,0);
  94. ret.add(message.substring(start,end));
  95. i = nextI;
  96. }*/
  97. int n = message.length();
  98. int start = 0, end = 0;
  99. while(true){
  100. if(end >= n){
  101. ret.add(message.substring(start,n-start));
  102. break;
  103. }
  104. if(end - start +1 > 160){
  105. if(message.charAt(end) != ' '){
  106. while(message.charAt(--end) != ' '){
  107.  
  108. }
  109. }
  110. ret.add(message.substring(Math.max(0,start),Math.max(0,end-start)));
  111. start = ++end;
  112. }else{
  113. ++end;
  114. }
  115. }
  116. return ret;
  117. }
  118.  
  119. }
  120.  
  121. public class Solution {
  122.  
  123.  
  124.  
  125. ____________________________________________
  126.  
  127. #include <bits/stdc++.h>
  128.  
  129. using namespace std;
  130.  
  131.  
  132.  
  133. /*
  134. * Complete the 'segments' function below.
  135. *
  136. * The function is expected to return a STRING_ARRAY.
  137. * The function accepts STRING message as parameter.
  138. */
  139.  
  140. vector<string> segments(string message) {
  141. int i = 0;
  142. vector<string> ret;
  143. int n = message.size();
  144. while(i<n){
  145. int start = i;
  146. i+=155;
  147. while(i<n && message[i] != ' '){
  148. --i;
  149. }
  150. int spaceAt = i;
  151. ret.push_back(message.substr(start,spaceAt+1));
  152. ++i;
  153. }
  154. int s = ret.size();
  155. for(int a=0;a<s;++a){
  156. string cur = "(" + to_string((a+1)) + "/" + to_string(s) + ")";
  157. ret[i] = ret[i].append(cur);
  158. }
  159. return ret;
  160. }
  161.  
  162. int main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement