编程介的小学生 2019-12-31 18:41 采纳率: 0.2%
浏览 49

Boring counting 的写法

Problem Description
035 now faced a tough problem,his english teacher gives him a string,which consists with n lower case letter,he must figure out how many substrings appear at least twice,moreover,such apearances can not overlap each other.
Take aaaa as an example.”a” apears four times,”aa” apears two times without overlaping.however,aaa can’t apear more than one time without overlaping.since we can get “aaa” from 0-2 and [1-3]. But the interval [0-2] and [1-3] overlaps each other.So “aaa” can not take into account.Therefore,the answer is 2(“a”,and “aa”).

Input
The input data consist with several test cases.The input ends with a line “#”.each test case contain a string consists with lower letter,the length n won’t exceed 1000(n <= 1000).

Output
For each test case output an integer ans,which represent the answer for the test case.you’d better use int64 to avoid unnecessary trouble.

Sample Input
aaaa
ababcabb
aaaaaa
#

Sample Output
2
3
3

  • 写回答

1条回答 默认 最新

  • 你知我知皆知 2024-07-27 09:23
    关注

    以下回答参考 皆我百晓生券券喵儿 等免费微信小程序相关内容作答,并由本人整理回复。

    # 读取测试数据
    test_data <- readLines()
    
    # 初始化答案列表
    ans_list <- numeric(length(test_data))
    
    # 遍历每个测试案例
    for (i in seq_along(test_data)) {
      # 提取当前测试案例的字符串
      current_str <- test_data[[i]][1:(length(test_data[[i]]) - 1)]
      
      # 计算所有可能的子串数量
      substring_count <- length(grep(current_str, current_str, fixed = TRUE))
      
      # 更新答案列表
      ans_list[i] <- substring_count
      
    }
    
    # 输出结果
    write.table(ans_list, file="output.txt", sep="\t", quote=F, col.names=TRUE)
    
    评论

报告相同问题?

悬赏问题

  • ¥15 已知平面坐标系(非直角坐标系)内三个点的坐标,反求两坐标轴的夹角
  • ¥15 webots有问题,无响应
  • ¥15 数据量少可以用MK趋势分析吗
  • ¥15 使用VH6501干扰RTR位,CANoe上显示的错误帧不足32个就进入bus off快慢恢复,为什么?
  • ¥15 大智慧怎么编写一个选股程序
  • ¥100 python 调用 cgps 命令获取 实时位置信息
  • ¥15 两台交换机分别是trunk接口和access接口为何无法通信,通信过程是如何?
  • ¥15 C语言使用vscode编码错误
  • ¥15 用KSV5转成本时,如何不生成那笔中间凭证
  • ¥20 ensp怎么配置让PC1和PC2通讯上