当前位置:   article > 正文

【PTA】L1-064 估值一亿的AI核心代码_pta估值一亿的ai核心代码

pta估值一亿的ai核心代码

https://pintia.cn/problem-sets/994805046380707840/problems/1111914599412858885

解析

题目不难,就正则替换一下就行。

要注意一下(已在代码中用注释说明):

1
can I can you
  • 1
  • 2
can I can you
AI: can you I can
  • 1
  • 2

AC Code

public static void main(String[] args) throws Exception {
    int n = nextInt();
    while(n-- != 0) {
        String text = br.readLine();
        String person = text;
        text = "";
        for(int i = 0; i < person.length(); i++) {
            char ch = person.charAt(i);
            if(ch != 'I') text += String.valueOf(ch).toLowerCase();
            else text += ch;
        }

        text = text.trim();
        text = text.replaceAll("\\s+", " ");

        // 加 “ _ ” 是为了防止与 I me 替换成 you 发生冲突
        text = text.replaceAll("\\bcan you\\b", "_I can");
        text = text.replaceAll("\\bcould you\\b", "_I could");

        text = text.replaceAll("\\bI\\b|\\bme\\b", "you");

        text = text.replaceAll("\\?", "!");
        text = text.replaceAll(" !", "!");
        text = text.replaceAll(" \\?", "?");
        text = text.replaceAll(" \\.", ".");
        text = text.replaceAll(" '", "'");
        text = text.replaceAll(" ,", ",");

        text = text.replaceAll("_I", "I");

        //text = text.replaceAll("\b_I\b", "");
        out.println(person);
        out.printf("AI: %s\n", text);
    }
    out.flush();
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号