Square Subsequence

前端 未结 5 490
盖世英雄少女心
盖世英雄少女心 2020-12-28 08:36

A string is called a square string if it can be obtained by concatenating two copies of the same string. For example, \"abab\", \"aa\" are square strings, while \"aaa\", \"a

5条回答
  •  鱼传尺愫
    2020-12-28 09:24

    I initially derive all possible sub-sequences and then i will check if the derived sub-sequence is a square sub-sequence or not

     import java.io.*;
     import java.util.*;
    
      public class Subsequence { 
      static int count;
      public static void print(String prefix, String remaining, int k) {
    
        if (k == 0) {
            //System.out.println(prefix);
            if(prefix.length() %2 == 0 && check(prefix) != 0 && prefix.length() != 0)
            {
                count++;
                //System.out.println(prefix);
            }
            return;
        }
        if (remaining.length() == 0) 
            return;
    
        print(prefix + remaining.charAt(0), remaining.substring(1), k-1);
        print(prefix, remaining.substring(1), k);
     }
    
    
     public static void main(String[] args) 
     {
        //String s = "aaa";
        Scanner sc = new Scanner(System.in);
        int t=Integer.parseInt(sc.nextLine());
        while((t--)>0)
        {
            count = 0;
            String s = sc.nextLine();
            for(int i=0;i<=s.length();i++)
            {
                 print("",s,i);
            }
            System.out.println(count);
         }
     }
    
     public static int check(String s)
     {
        int i=0,j=(s.length())/2;
    
        for(;i<(s.length())/2 && j < (s.length());i++,j++)
        {
            if(s.charAt(i)==s.charAt(j))
            {
                    continue;
            }
    
            else
               return 0;
        }
    
        return 1;
    }
    
    }
    

提交回复
热议问题