赞
踩
https://pintia.cn/problem-sets/994805046380707840/problems/1111914599412858885
题目不难,就正则替换一下就行。
要注意一下(已在代码中用注释说明):
1
can I can you
can I can you
AI: can you I can
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(); }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。