估值一億的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;
/**
*
* @author 梓葉楓林
* @date 2020/10/30
*/
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();
/*
* 33-66行都是在寫正則運算式
* */
//洗掉多余的空格
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);
}
}
/*
* 90-128行都是在使用前面的正則運算式來進行匹配替換
* */
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);
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/200680.html
標籤:java
上一篇:JS入門起步
下一篇:ajax異步實作檔案分片上傳
