LoginSignup
2
2

More than 5 years have passed since last update.

《結城浩のスペーストーキー問題》

Posted at

Main.java
import java.io.BufferedReader;
import java.io.InputStreamReader;

class Main {
  private static String encodeStringFromChar2(char c, int n) {
    StringBuffer buf = new StringBuffer();
    for(int i = 0; n > 0 ; i++) {
      buf.append(c); 
      buf.append((char)('a' + ((n > 26)? 25 : n - 1)));
      n -= 26;
    }
    return buf.toString();
  }
  private static String myEncode(char[] str, int n) {
    StringBuffer buf = new StringBuffer();
    int i = 0;
    while(i < n) {
       char c = str[i];
       int  m = i;
       while(i < n && c == str[i]) i++; 
       buf.append(encodeStringFromChar2(c, i - m));
    }
    return buf.toString();
  }
  private static String myDecode(char[] str, int n) {
    StringBuffer buf = new StringBuffer();
    for(int i = 0; i < n; i++) {
       char c = str[i++];
       int  m = str[i] - 'a' + 1;
       buf.append(decodeStringFromChar(c, m));
    }
    return buf.toString();
  }
  private static String decodeStringFromChar(char c, int n) {
    char[] value = new char[n];
    for(int i = 0; i < n; i++) value[i] = c;  
    return new String(value);
  }
  public static void main(String[] args) throws Exception {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    String line = null;
    while((line = br.readLine()) != null) {
      int n = line.length();
      char[] a = line.toCharArray();
      if(n % 2 != 0) System.out.println("X:" + line);
      else {
       String decoded = myDecode(a, n);
       int m = decoded.length();
       boolean isValid = m <= 1024 && 
                         line.equals(myEncode(decoded.toCharArray(), m));
       if(isValid)
         System.out.println(decoded + ":" + line);
       else
         System.out.println("X:" + line);
      }
    } 
  }
}
2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2