pku3371 Flesch Reading Ease 字符串处理

题目很罗嗦,说到底干三件事:
1、统计句子的个数(以".:;!?"为分隔符),这个只要统计分隔符的个数即可
2、统计单词的个数,这个需要注意一点,不能仅仅以空格来spilt字符串,然后统计个数的方法。。只有除去标点符号后非空的patten才能算一个word
3、统计音节的个数。
  1. -es, -ed and -e (except -le) endings are ignored, (末尾以es、ed、e(并且倒数第二个字母不是l)结束的部分不算在内)
  2. words of three letters or shorter count as single syllables, (字母个数(不是patten的长度,应为可能有apple-pile这种类型的复合词)小于3的单词都算一个音节,不一定要包括aeiouy)
  3. consecutive vowels count as one syllable.(连续的音标算一个音节)

所以,最好在统计前将patten内的所有标点符号都去除掉
代码:

 1Source Code
 2
 3Problem: 3371  User: yzhw 
 4Memory: 3004K  Time: 313MS 
 5Language: Java  Result: Accepted 
 6
 7Source Code 
 8import java.io.*;
 9public class Main {
10
11    /**
12     * @param args
13     */

14    static boolean IsSyllable(char ch)
15    {
16        switch(ch)
17        {
18        case 'a':return true;
19        case 'e':return true;
20        case 'i':return true;
21        case 'o':return true;
22        case 'u':return true;
23        case 'y':return true;
24        default:return false;
25        }

26    }

27    public static void main(String[] args) throws IOException{
28        BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
29        int sen=0,word=0,syl=0;
30        while(true)
31        {
32            String tmp=in.readLine();
33            if(tmp==nullbreak;
34            String tok[]=tmp.split(" ");
35            for(int snum=0;snum<tok.length;snum++)
36            {
37                String str=tok[snum].toLowerCase();
38                while(!str.isEmpty()&&!Character.isLetter(str.charAt(0)))
39                {
40                    switch(str.charAt(0))
41                    {
42                    case '.':sen++;break;
43                    case '?':sen++;break;
44                    case '!':sen++;break;
45                    case ':':sen++;break;
46                    case ';':sen++;break;
47                    }
;
48                    str=str.substring(1);
49                }

50                while(!str.isEmpty()&&!Character.isLetter(str.charAt(str.length()-1)))
51                {
52                    switch(str.charAt(str.length()-1))
53                    {
54                        case '.':sen++;break;
55                        case '?':sen++;break;
56                        case '!':sen++;break;
57                        case ':':sen++;break;
58                        case ';':sen++;break;
59                    }
;
60                    str=str.substring(0, str.length()-1);
61                }

62                for(int i=0;i<str.length();i++)
63                {
64                   if(!Character.isLetter(str.charAt(i)))
65                       str=str.substring(0,i)+str.substring(i+1);
66                }

67                if(!str.isEmpty()) word++;
68                else continue;
69                int end=str.length();
70                if(str.endsWith("es")||str.endsWith("ed")) end-=2;
71                else if(str.endsWith("e")&&!str.endsWith("le")) end--;
72                if(str.length()<=3)
73                {
74                    syl++;
75                }

76                else
77                    for(int i=0;i<end;i++)
78                        if(i==0&&IsSyllable(str.charAt(i))||i!=0&&!IsSyllable(str.charAt(i-1))&&IsSyllable(str.charAt(i)))
79                            syl++;
80            }

81        }

82        //System.out.println(sen+" "+word+" "+syl);
83        System.out.printf("%.2f\n"206.835-1.015*word/sen-84.6*syl/word);
84
85    }

86
87}

88
89

posted on 2011-01-06 23:26 yzhw 阅读(521) 评论(0)  编辑 收藏 引用 所属分类: string algorithm


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


<2024年5月>
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678

导航

统计

公告

统计系统

留言簿(1)

随笔分类(227)

文章分类(2)

OJ

最新随笔

搜索

积分与排名

最新评论

阅读排行榜