估值一亿的AI核心代码 PTA
- 题目
-
- 输入格式:
- 输出格式:
- 输入样例:
- 输出样例:
- 分析
-
- 解题思路:
- 答案
题目
以上图片来自新浪微博。
本题要求你实现一个稍微更值钱一点的 AI 英文问答程序,规则是:
- 无论用户说什么,首先把对方说的话在一行中原样打印出来;
- 消除原文中多余空格:把相邻单词间的多个空格换成 1 个空格,把行首尾的空格全部删掉,把标点符号前面的空格删掉;
- 把原文中所有大写英文字母变成小写,除了 I;
- 把原文中所有独立的 can you、could you 对应地换成 I can、I could —— 这里“独立”是指被空格或标点符号分隔开的单词;
- 把原文中所有独立的 I 和 me 换成 you;
- 把原文中所有的问号 ? 换成惊叹号! ;
- 在一行中输出替换后的句子作为 AI 的回答。
输入格式:
输入首先在第一行给出不超过 10 的正整数 N,随后 N 行,每行给出一句不超过 1000 个字符的、以回车结尾的用户的对话,对话为非空字符串,仅包括字母、数字、空格、可见的半角标点符号。
输出格式:
按题面要求输出,每个 AI 的回答前要加上 AI: 和一个空格。
输入样例:
6
Hello ?
Good to chat with you
can you speak Chinese?
Really?
Could you show me 5
What Is this prime? I,don 't know
输出样例:
Hello ?
AI: hello!
Good to chat with you
AI: good to chat with you
can you speak Chinese?
AI: I can speak chinese!
Really?
AI: really!
Could you show me 5
AI: I could show you 5
What Is this prime? I,don 't know
AI: what Is this prime! you,don’t know
分析
本题使用正则表达式来解题会简单许多。
解题思路:
1.先转换大小写,将所有的大写字母转换成小写字母,除了大写的I。
2.使用正则表达式来替换对应的单词
3.注意:在最后的时候我们需要先把could you 和 can you 转换成I could 和 I can,再把 I 和 me 转换成you。否则会出现下面的情况
输入:
1
Can I do it ?
输出:
Can I do it ?
AI: I can do it!
而正确的输出应该是:
Can I do it ?
AI: can you do it!
因此我们应该先把could you 和 can you 转换成I could 和 I can,再把 I 和 me 转换成you。可是这样的话我们的I也会被转换成you,所以我们可以把I给做一个标记,比如把could you 和 can you 转换成_I could 和 _I can,最后我们再把_I给转换成I。
答案
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main (String [] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
List<String> whatUserSaysStr = new ArrayList<>(n);
//读取回车符
scanner.nextLine();
//把用户说的话存起来
for (int i = 0; i < n; i++) {
whatUserSaysStr.add(scanner.nextLine());
}
scanner.close();
//删除多余的空格
String removeDuplicateSpaceRegex = "\\s+";
Pattern dupPattern = Pattern.compile(removeDuplicateSpaceRegex);
//删除,前的空格
String removeSpaceBeforeDommaRegex = " ,";
Pattern dommaPattern = Pattern.compile(removeSpaceBeforeDommaRegex);
//删除.前的空格
String removeSpaceBeforePeriodRegex = " \\.";
Pattern periodPattern = Pattern.compile(removeSpaceBeforePeriodRegex);
//删除!前的空格
String removeSpaceBeforeExcRegex = " !";
Pattern excPattern = Pattern.compile(removeSpaceBeforeExcRegex);
//删除?前的空格
String removeSpaceBeforeQuesRegex = " \\?";
Pattern quesPattern = Pattern.compile(removeSpaceBeforeQuesRegex);
//删除'前的空格
String removeSpaceBeforeQuotRegex = " '";
Pattern quotPattern = Pattern.compile(removeSpaceBeforeQuotRegex);
//替换?成!
String replaceQuesRegex = "\\?";
Pattern replaceQuesPattern = Pattern.compile(replaceQuesRegex);
//替换I成you
String replaceIRegex = "\\bI\\b";
Pattern replaceIPattern = Pattern.compile(replaceIRegex);
//替换me成you
String replaceMeRegex = "\\bme\\b";
Pattern replaceMePattern = Pattern.compile(replaceMeRegex);
//替换can you成I can
String replaceCanRegex = "\\bcan you\\b";
Pattern replaceCanPattern = Pattern.compile(replaceCanRegex);
//替换could you成I could
String replaceCouldRegex = "\\bcould you\\b";
Pattern replaceCouldPattern = Pattern.compile(replaceCouldRegex);
for (int i = 0; i < whatUserSaysStr.size(); i++) {
//输出原话
System.out.println(whatUserSaysStr.get(i));
//去掉首尾的空格
StringBuilder ansStr = new StringBuilder(whatUserSaysStr.get(i).trim());
//字母转小写,除了I
for (int j = 0; j < ansStr.length(); j++) {
char currentChar = ansStr.charAt(j);
if ( (currentChar >= 'A' && currentChar < 'I') || (currentChar > 'I' && currentChar <= 'Z') ) {
currentChar += 32;
ansStr.setCharAt(j, currentChar);
}
}
Matcher matcher1 = dupPattern.matcher(ansStr);
String s1 = matcher1.replaceAll(" ");
Matcher matcher2 = dommaPattern.matcher(s1);
String s2 = matcher2.replaceAll(",");
Matcher matcher3 = periodPattern.matcher(s2);
String s3 = matcher3.replaceAll(".");
Matcher matcher4 = excPattern.matcher(s3);
String s4 = matcher4.replaceAll("!");
Matcher matcher5 = quesPattern.matcher(s4);
String s5 = matcher5.replaceAll("?");
Matcher matcher6 = quotPattern.matcher(s5);
String s6 = matcher6.replaceAll("'");
Matcher matcher7 = replaceQuesPattern.matcher(s6);
String s7 = matcher7.replaceAll("!");
Matcher matcher8 = replaceCanPattern.matcher(s7);
String s8 = matcher8.replaceAll("_I can");
Matcher matcher9 = replaceCouldPattern.matcher(s8);
String s9 = matcher9.replaceAll("_I could");
Matcher matcher10 = replaceIPattern.matcher(s9);
String s10 = matcher10.replaceAll("you");
Matcher matcher11 = replaceMePattern.matcher(s10);
String s11 = matcher11.replaceAll("you");
String replaceIIRegex = "\\b_I\\b";
Pattern replaceIIPattern = Pattern.compile(replaceIIRegex);
Matcher matcher12 = replaceIIPattern.matcher(s11);
String ans = matcher12.replaceAll("I");
System.out.println("AI: " + ans);
}
}
}