以上图片来自新浪微博。
本题要求你实现一个稍微更值钱一点的 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
java解答
import java.util.Scanner;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = Integer.parseInt(sc.nextLine());
for(int i=0;i<n;i++){
String str = sc.nextLine();
System.out.println(str);
str = str.trim();
char[] arr = str.toCharArray();
StringBuilder stringBuilder = new StringBuilder();
Pattern pattern = Pattern.compile("\\pP");
for(int j=0;j<arr.length;j++){
if(arr[j]>='A'&&arr[j]<='Z'&&arr[j]!='I'){
stringBuilder.append((char)(arr[j]+32));
}else if(arr[j]==' '){
if(arr[j+1]!=' '){
if(!pattern.matcher(String.valueOf(arr[j+1])).matches()){
stringBuilder.append(arr[j]);
}
}
}else if(arr[j]=='?'){
stringBuilder.append('!');
}else {
stringBuilder.append(arr[j]);
}
}
StringBuilder stringBuilder1 = new StringBuilder();
char[] arr1 = (" "+stringBuilder.toString()).toCharArray();
for(int j=0;j<arr1.length;j++){
if(arr1[j]==' '||pattern.matcher(String.valueOf(arr1[j])).matches()||j==0){
if(j+1<=arr1.length-1){
if(j+2==arr1.length){
if(arr1[j + 1] == 'I'){
stringBuilder1.append(arr1[j] + "you");
j = j + 1;
continue;
}
}else{
if(arr1[j + 1] == 'I'){
if (arr1[j + 2] == ' ' || pattern.matcher(String.valueOf(arr1[j + 2])).matches()) {
stringBuilder1.append(arr1[j] + "you");
j = j + 1;
continue;
}
}
}
if(j+2<=arr1.length-1) {
if (j + 3 == arr1.length) {
if (arr1[j + 1] == 'm') {
if (arr1[j + 2] == 'e') {
stringBuilder1.append(arr1[j] + "you");
j = j + 2;
continue;
}
}
if(arr1[j + 1]=='I'){
stringBuilder1.append(arr1[j] + "you");
j = j + 1;
continue;
}
} else {
if (arr1[j + 1] == 'm') {
if (arr1[j + 2] == 'e') {
if (arr1[j + 3] == ' ' || pattern.matcher(String.valueOf(arr1[j + 3])).matches()) {
stringBuilder1.append(arr1[j] + "you");
j = j + 2;
continue;
}
}
}
}
if (j + 7 <= arr1.length - 1) {
if (j + 8 == arr1.length) {
if (arr1[j + 1] == 'c') {
if (arr1[j + 2] == 'a') {
if (arr1[j + 3] == 'n') {
if (arr1[j + 4] == ' ') {
if (arr1[j + 5] == 'y') {
if (arr1[j + 6] == 'o') {
if (arr1[j + 7] == 'u') {
stringBuilder1.append(arr1[j] + "I can");
j = j + 7;
continue;
}
}
}
}
}
}
}
} else {
if (arr1[j + 1] == 'c') {
if (arr1[j + 2] == 'a') {
if (arr1[j + 3] == 'n') {
if (arr1[j + 4] == ' ') {
if (arr1[j + 5] == 'y') {
if (arr1[j + 6] == 'o') {
if (arr1[j + 7] == 'u') {
if (arr1[j + 8] == ' ' || pattern.matcher(String.valueOf(arr1[j + 8])).matches()) {
stringBuilder1.append(arr1[j] + "I can");
j = j + 7;
continue;
}
}
}
}
}
}
}
}
}
}
if (j + 9 <= arr1.length - 1) {
if (j + 10 == arr1.length) {
if (arr1[j + 1] == 'c') {
if (arr1[j + 2] == 'o') {
if (arr1[j + 3] == 'u') {
if (arr1[j + 4] == 'l') {
if (arr1[j + 5] == 'd') {
if (arr1[j + 6] == ' ') {
if (arr1[j + 7] == 'y') {
if (arr1[j + 8] == 'o') {
if (arr1[j + 9] == 'u') {
stringBuilder1.append(arr1[j] + "I could");
j = j + 9;
continue;
}
}
}
}
}
}
}
}
}
} else {
if (arr1[j + 1] == 'c') {
if (arr1[j + 2] == 'o') {
if (arr1[j + 3] == 'u') {
if (arr1[j + 4] == 'l') {
if (arr1[j + 5] == 'd') {
if (arr1[j + 6] == ' ') {
if (arr1[j + 7] == 'y') {
if (arr1[j + 8] == 'o') {
if (arr1[j + 9] == 'u') {
if (arr1[j + 10] == ' ' || pattern.matcher(String.valueOf(arr1[j + 10])).matches()) {
stringBuilder1.append(arr1[j] + "I could");
j = j + 9;
continue;
}
}
}
}
}
}
}
}
}
}
}
}
}
}
stringBuilder1.append(arr1[j]);
}else{
stringBuilder1.append(arr1[j]);
}
}
String str1 = stringBuilder1.toString().trim();
System.out.println("AI: "+str1);
}
}
}