蓝桥杯真题-人物相关性分析

题目描述
小明正在分析一本小说中的人物相关性。他想知道在小说中 Alice 和 Bob 有多少次同时出现。
更准确的说,小明定义 Alice 和 Bob“同时出现”的意思是:在小说文本 中 Alice 和 Bob 之间不超过 K 个字符。
例如以下文本:
ThisisastoryaboutAliceandBob.AlicewantstosendaprivatemessagetoBob.
假设 K = 20,则 Alice 和 Bob 同时出现了 2 次,分别是”Alice and Bob” 和”Bob. Alice”。前者 Alice 和 Bob 之间有 5 个字符,后者有 2 个字符。
注意:

Alice 和 Bob 是大小写敏感的,alice 或 bob 等并不计算在内。
Alice 和 Bob 应为单独的单词,前后可以有标点符号和空格,但是不能 有字母。例如 Bobbi 並不算出现了 Bob。
【输入格式】
第一行包含一个整数 K。
第二行包含一行字符串,只包含大小写字母、标点符号和空格。长度不超 过 1000000。
【输出格式】
输出一个整数,表示 Alice 和 Bob 同时出现的次数。
【样例输入】
20
ThisisastoryaboutAliceandBob.AlicewantstosendaprivatemessagetoBob.
【样例输出】
2
【评测用例规模与约定】
对于所有评测用例,1≤ K ≤1000000。
题目分析
题目代码
————————————————
 

import java.util.Scanner;

public class a24 {

    public static String test;
    public static int len;
    public static int k;
    public static String w1,w2;
    public static int l1,l2;
    public static int index1,index2;

    public static void main(String[] args) {

        k = 20;
        //This is a story about
        test = "ThisisastoryaboutAliceandBob.AlicewantstosendaprivatemessagetoBob.";
        len = test.length();

        w1 = "Alice";
        w2 = "Bob";


        int i;
        for (i = 0; i < len; i++) {

//            if (Math.abs(index1 - index2) > k)
//                break;

                if (test.charAt(i) == 'A') {
                    dfsa(0,i);
                }else if (test.charAt(i) == 'B') {
                    dfsb(0,i);
                }

        }


        System.out.println(Math.max(l1,l2));

    }

    public static void dfsa(int i,int index) {

        if (index >= test.length()) {
            return;
        }

        if (test.charAt(index) != w1.charAt(i))
            return;

        if (i == w1.length()-1) {

            if (index1 == 0) {
                l1++;
                index1 = index;
                return;
            }else if (l1 == l2) {

                l1++;
                index1 = index;
                return;
            }else if (l1 != l2 && index2 > index1 && index - index2 <= k) {

                l1++;
                index1 = index;
                return;
            }
            return;
        }

        dfsa(i+1,index+1);

    }

    public static void dfsb(int i,int index) {

        if (index >= test.length()) {
            return;
        }

        if (test.charAt(index) != w2.charAt(i))
            return;

        if (i == w2.length()-1) {

            if (index2 == 0) {
                l2++;
                index2 = index;
                return;
            }else if (l1 == l2) {

                l2++;
                index2 = index;
                return;
            }else if (l1 != l2 && index1 > index2 && index - index1 <= k) {

                l2++;
                index2 = index;
                return;
            }
            return;
        }

        dfsb(i+1,index+1);
    }

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

海边的彩虹与你

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值