Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- class MacString{
- LinkedList<Character> string;
- public MacString(){
- string = new LinkedList<Character>();
- }
- public MacString(String s){
- string = new LinkedList<Character>();
- for(char a : s.toCharArray()) string.add(a);
- }
- public MacString(LinkedList<Character> string){
- this.string = string;
- }
- public void append(char c){
- string.add(c);
- }
- public void insert(char c){
- string.addFirst(c);
- }
- public int length(){
- return string.size();
- }
- public boolean isPalindrome(){
- for(int i=0, j=string.size()-1;i<j;i++,j--){
- if(!string.get(i).equals(string.get(j))) return false;
- }
- return true;
- }
- public MacString changeAplhabet(Map<Character, Character> mapa){
- MacString str = new MacString(string);
- Iterator it = mapa.entrySet().iterator();
- while(it.hasNext()){
- Map.Entry pairs = (Map.Entry)it.next();
- if(str.string.contains(pairs.getKey()))
- for(int i=0;i<str.string.size();i++){
- if(str.string.get(i).equals(pairs.getKey()))
- str.string.set(i, (Character) pairs.getValue());
- }
- }
- return str;
- }
- public MacString format(int length, boolean right){
- MacString str = new MacString();
- if(length<string.size()) return new MacString(string);
- else{
- if(right){
- for(int i=0;i<(length-string.size()); i++) str.append(' ');
- for(int i=0;i<string.size();i++) str.append(string.get(i));
- }
- else{
- for(int i=0;i<string.size();i++) str.append(string.get(i));
- for(int i=0;i<(length-string.size()); i++) str.append(' ');
- }
- }
- return str;
- }
- public boolean isRecuring(MacString base){
- String s = "";
- for(int i=0;i<string.size();i++) s+=string.get(i);
- if(s.contains(base.toString())){
- int pocetok = s.indexOf(base.toString());
- s = s.substring(0, pocetok) + s.substring(pocetok + base.length(), s.length());
- if(s.contains(base.toString())) return true;
- }
- return false;
- }
- public String toString(){
- String s = "";
- for(int i=0;i<string.size();i++) s+=string.get(i);
- return s;
- }
- }
- public class MacStringTest {
- public static void main(String[] args) {
- Scanner jin = new Scanner(System.in);
- MacString mc = new MacString(jin.next());
- int k = jin.nextInt();
- if ( k == 0 ) {//test eve but changeAplhabet
- while ( true ) {
- String cmd = jin.next();
- if ( cmd.equals("stop") ) break;
- System.out.print(cmd+" ");
- if ( cmd.equals("isPalindrome") ) {
- System.out.println();
- System.out.println(mc.isPalindrome());
- System.out.println(mc);
- }
- if ( cmd.equals("isRecuring") ) {
- String base = jin.next();
- System.out.println(base);
- System.out.println(mc.isRecuring(new MacString(base)));
- System.out.println(mc);
- }
- if ( cmd.equals("insert") ) {
- char c = jin.next().charAt(0);
- System.out.println(c);
- mc.insert(c);
- }
- if ( cmd.equals("append") ) {
- char c = jin.next().charAt(0);
- System.out.println(c);
- mc.append(c);
- }
- if ( cmd.equals("format") ) {
- int n = jin.nextInt();
- boolean asc = jin.nextBoolean();
- System.out.println(n+" "+asc);
- System.out.println(mc.format(n,asc));
- System.out.println(mc);
- }
- if ( cmd.equals("print") ) {
- System.out.println();
- System.out.println(mc);
- }
- }
- }
- if ( k == 1 ) {//test changeAlphabet
- int num_rules = jin.nextInt();
- TreeMap<Character,Character> rules = new TreeMap<Character,Character>();
- for ( int i = 0 ; i < num_rules ; ++i ) {
- String rule = jin.next();
- rules.put(rule.charAt(0), rule.charAt(2));
- }
- System.out.println(mc.changeAplhabet(rules));
- System.out.println(mc);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement