AcWing 1557. 说话方式java(字符串java会丢掉几分) 不过问题不是很大
原题链接
简单
作者:
mkuiwu
,
2020-12-28 23:52:55
,
所有人可见
,
阅读 519
import java.lang.*;
import java.io.*;
import java.util.*;
class Main{
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static StringTokenizer stz = new StringTokenizer("");
static String nextLine() throws Exception {// 读取下一行字符串
return br.readLine();
}
static String next() throws Exception {// 读取下一个字符串
while (!stz.hasMoreTokens()) {
stz = new StringTokenizer(br.readLine());
}
return stz.nextToken();
}
static int nI() throws Exception {// 读取下一个int型数值
return Integer.parseInt(next());
}
static double nD() throws Exception {// 读取下一个double型数值
return Double.parseDouble(next());
}
static long nL() throws Exception {// 读取下一个double型数值
return Long.parseLong(next());
}
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
public void run() throws Exception{
String str = nextLine().toLowerCase();
Map<String, Integer> map = new HashMap<>();
String res = new String();
int count = -1;
StringBuffer temp = new StringBuffer();
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
boolean flat= isOk(c);
if (flat) temp.append(c);
if (!flat || i==str.length()-1){
String string = temp.toString();
temp = new StringBuffer();
if (string.equals("")) continue;
if (map.get(string) == null) map.put(string, 1);
else {
map.put(string, map.get(string)+1);
}
int cnt = map.get(string);
if (cnt > count || cnt == count && string.compareTo(res) < 0 ){
count = cnt;
res = string;
}
}
}
bw.write(res+' '+count);
bw.flush();
}
public boolean isOk(char c){
if (c-'0' >= 0 && c-'0' <= 9) return true;
if (c >= 'a' && c <= 'z') return true;
return false;
}
public static void main(String[] args) throws Exception{
new Main().run();
}
}