题目描述
Hard to force the CET4&6 is imminent, which makes most of the students a headache. This does not, Liu is trying his best to prepare for the CET6, trying to do a variety of previous year’s problem. But the most helpless for Liu is a quick read of this topic, so he thought of a thing does not make sense is that the statistical article the total number in different words. Here your task is to help Liu solve this problem.
输入
Multiple sets of data, each row is an article. Each article is consisted by lowercase letters and spaces, no punctuation, encounter # when the input end.
输出
Each group output only an integer, which alone make the trip, the integer representing the total number of different words in an article.
样例输入
you are my friend can you can a can as a canner can can a can #
样例输出
4 5
【AC代码】:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
String s = sc.nextLine();
if (s.equals("#"))
break;
String c[] = new String[100];
int x = 0, y = 0, k = 0;
for (int i = 0; i < s.length(); i++)
if (s.charAt(i) == ' ') {
y = i;
c[k] = s.substring(x, y);
x = y + 1;
k++;
}
c[k] = s.substring(x, s.length());//将输入的一行字符串按空格分开
int n = 0;//记录不同单词总个数
int p[] = new int[k + 1];
for (int i = 0; i <= k; i++)
for (int j = 0; j < i; j++)
if (c[j].equals(c[i]))
p[i] = 1;//若单词已出现再次出现则记为1
for (int i = 0; i <= k; i++)
if (p[i] != 1)//单词未重复出现过总个数加一
n++;
System.out.println(n);
}
}
}