Blue Bridge Cup-Password drop (java)

Passwords come off
The archaeologists of Planet X have discovered a batch of codes left by ancient times. These codes are a sequence of seeds of four plants A, B, C, and D. Careful analysis found that these password strings should be symmetrical at the beginning (that is, the mirror string we said). Due to the age, many of the seeds have fallen off, and they may lose their mirror image characteristics.
Your task is: given a password string that you see now, calculate how many seeds it has to shed from its original state before it may become what it is now.
Enter a line, indicating that the password string you see now (the length is not greater than 1000) requires a positive integer to be output, indicating how many seeds are at least dropped.
For example, input: ABCBA
, the program should output: 0.
For example, input: ABDCDCBABC
, the program should output: 3
Resource convention: peak memory consumption <256MCPU consumption <1000ms

Use double pointers to solve
less use case verification, please correct me if there is a problem

import java.util.Scanner;
public class Mimatl {
//回文数密码脱落问题 
    static int check(String a)
    {
     char []b=a.toCharArray();//转为字符串
     int start=0;
     int end=b.length-1;
     int count=0;
     while(start<=end)
     {
      if(b[start]!=b[end])//若不相等
      {
       count++;//计数加一
       boolean s=true;//设定的一个标志位;巧用标志位判断该段程序是否执行
          for(int i=end-1;i>(end-start)>>1;i--)//循环确定是start++还是end--;
          {
           if(b[i]==b[start])
           {
            end--;
            s=false;
            break;
            }             
          }
         if(s==true)
          start++;
      }
      else
      {
         start++;
         end--;
      }
     }
     return count;
    }
    public static void main(String[] args) {
  // TODO Auto-generated method stub
 Scanner a=new Scanner(System.in);
 String b=a.nextLine();
 int c=check(b);
 System.out.println(c);
 }
 }
 





Published 14 original articles · praised 0 · visits 245

Guess you like

Origin blog.csdn.net/lianggege88/article/details/105079927