zoukankan      html  css  js  c++  java
  • 贝壳笔试——字符串构造

    题目:略

    分析:问题的本质是判断目标字符串在构建过程中是否可以使用一次复制粘贴。

      1.问题限制:字符串的复制粘贴只能使用一次,且需要从最开始复制。

      2.首先判断目标字符的长度是否为偶数,不是偶数则减一。

      3.从后往前检索,每次回退2个单位。如果满足条件就直接退出。

    code:

     1 import java.util.*;
     2 
     3 public class Main {
     4     public static void main(String args[]) {
     5         Scanner read = new Scanner(System.in);
     6         int n = read.nextInt();
     7         read.nextLine();
     8         String line = read.nextLine();
     9         int count = 0;
    10         //不是偶数长度,变为偶数长度,记录器+1
    11         if (n % 2 != 0) {
    12             count++;
    13             n--;
    14         }
    15         for (int i = n; i > 0; i = i - 2) {
    16             int tmp = i / 2;    //计算切割点
    17             String str1 = line.substring(0, tmp);
    18             String str2 = line.substring(tmp, i);
    19             //判断是否满足复制粘贴条件
    20             if (str1.equals(str2)) {
    21                 count += str1.length() + 1;
    22                 break;
    23             }
    24             count += 2;
    25         }
    26         System.out.println(count);
    27 
    28     }
    29 }
    View Code
  • 相关阅读:
    poj 3087 直接模拟
    POJ-3126 BFS,埃式筛选及黑科技
    POJ3278-Catch That Cow
    js变量提升
    饿了么
    2分钟就能学会的【Google/百度搜索大法】了解一下?
    span标签间距
    Vue移动端项目如何使用手机预览调试
    Port 3000 is already in use
    koa2第一天 async详解
  • 原文地址:https://www.cnblogs.com/dream-flying/p/13629101.html
Copyright © 2011-2022 走看看