Advertisement
shek15470

Untitled

Mar 14th, 2019
266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.48 KB | None | 0 0
  1. import java.util.Iterator;
  2. import java.util.NoSuchElementException;
  3.  
  4. public class Sentence implements Iterable<String> {
  5.  
  6.     private String s;
  7.  
  8.     Sentence(String s) {
  9.         this.s = s;
  10.     }
  11.  
  12.     @Override
  13.     public Iterator<String> iterator() {
  14.         return new PalindromeIterator();
  15.     }
  16.  
  17.     @Override
  18.     public String toString() {
  19.         return s;
  20.     }
  21.  
  22.     private class PalindromeIterator implements Iterator<String> {
  23.  
  24.         private int pos;
  25.  
  26.         PalindromeIterator() {
  27.             pos = 0;
  28.         }
  29.  
  30.         @Override
  31.         public boolean hasNext() {
  32.             while (true) {
  33.                 while (pos < s.length() && s.charAt(pos) == ' ') {
  34.                     pos++;
  35.                 }
  36.                 if (pos == s.length()) {
  37.                     return false;
  38.                 }
  39.                 int i = pos, j = 0;
  40.                 while (i < s.length() && s.charAt(i) != ' ') {
  41.                     i++;
  42.                 }
  43.                 i--;
  44.                 while (pos + j < i - j && s.charAt(pos + j) == s.charAt(i - j)) {
  45.                     j++;
  46.                 }
  47.                 if (pos + j >= i - j) {
  48.                     return true;
  49.                 } else {
  50.                     pos = i + 2;
  51.                 }
  52.             }
  53.         }
  54.  
  55.         @Override
  56.         public String next() {
  57.             while (true) {
  58.                 while (pos < s.length() && s.charAt(pos) == ' ') {
  59.                     pos++;
  60.                 }
  61.                 if (pos == s.length()) {
  62.                     throw new NoSuchElementException();
  63.                 }
  64.                 int i = pos, j = 0;
  65.                 while (i < s.length() && s.charAt(i) != ' ') {
  66.                     i++;
  67.                 }
  68.                 i--;
  69.                 while (j * 2 < i - pos && s.charAt(pos + j) == s.charAt(i - j)) {
  70.                     j++;
  71.                 }
  72.                 j = pos;
  73.                 pos = i + 2;
  74.                 if (pos + j >= i - j) {
  75.                     return s.substring(j, i + 1);
  76.                 }
  77.             }
  78.         }
  79.  
  80.     }
  81.  
  82. }
  83. public class Test {
  84.  
  85.     public static void main(String[] args) {
  86.         Sentence s = new Sentence("hello   world x  abcdcba 1232321  1243421        11  ");
  87.         System.out.println("Предложение: " + s);
  88.         System.out.print("Палиндромы:");
  89.         for (String palindrome : s) {
  90.             System.out.print("  " + palindrome);
  91.         }
  92.     }
  93.  
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement