1319 單詞統計

來源:互聯網
上載者:User
描述

         給定一段文章,每行不超過1000個字元,統計文章中每個單詞出現的頻率,忽略大小寫差異。

注意:單詞的不同形態看做是不同的單詞,不考慮連字號,所有非大小寫字母都視為只起到分割作用。

輸入

         第一行為一個整數T,表示資料的組數。

         對於每組資料以一個獨佔一行的句號('.')結束。

輸出

         對於每組測試資料:

第一行為一個整數n表示文章中出現的單詞的總數;

接下來n行按字母序從小到大輸出文章中出現的單詞(全部以大寫字母表示)和出現的頻數。

範例輸入
2
Hello World!
.
Abc abc-cde aa1bb2cc3 aas’ (test)...
.
範例輸出
2
HELLO 1
WORLD 1
7
AA 1
AAS 1
ABC 2
BB 1
CC 1
CDE 1
TEST 1

此題是簡單的統計,然後排序,但要注意以下幾點:

首先, 每組資料沒說只有一行, 只是說每一行不超過1000個字元, 所以一行一行讀, 直到讀到一行是'.'結束
第二, 當一行的最後一個字元是字母的時候, 你沒有處理這最後一個單詞, 比如這一行是"ABC.ABC", 那麼結果應該是ABC 2, 但是你的程式會輸出ABC 1, 因為你沒處理最後一個單詞ABC, 原因是因為你只有當出現不是字母的時候才處理前面一個單詞, 但是結尾如果就是字母那麼就出錯了

#include <stdio.h>#include <string.h>int main(){    int t,number;    int i,j;    int k,l;    char a[1000][1001];    int b[1000];    char temp2[1001];    char temp[1001];    int temp3;    int length;    char en[3];    scanf("%d",&number);    getchar();    for(t=1;t<=number;t++)    {        k=0;        l=0;        memset(a,'\0',sizeof(a));        memset(b,0,sizeof(b));        while(true)        {            gets(temp);            if(temp[0] == '.' &&strlen(temp) == 1) { break;}            length=strlen(temp);            for(i=0;i<length;i++)            {                if(temp[i]>='A'&&temp[i]<='Z')                {                    temp2[l++]=temp[i];                }                else if(temp[i]>='a'&&temp[i]<='z')                {                    temp2[l++]=temp[i]-32;                }                else                {                    temp2[l]='\0';                    if(l==0) continue;                    for(j=0;j<k;j++)                    {                        if(strcmp(a[j],temp2)==0)                        {                            b[j]++;                            break;                        }                    }                    if(j==k)                    {                        strcpy(a[j],temp2);                        b[j]++;                        k++;                    }                    l=0;                }            }            temp2[l]='\0';            if(l==0) continue;            for(j=0;j<k;j++)            {                if(strcmp(a[j],temp2)==0)                {                    b[j]++;                    break;                }            }            if(j==k)            {                strcpy(a[j],temp2);                b[j]++;                k++;            }            l=0;        }        for(i=0;i<k;i++)            for(j=i+1;j<k;j++)            {                if(strcmp(a[i],a[j])>0)                {                    strcpy(temp2,a[j]);                    temp3=b[j];                    strcpy(a[j],a[i]);                    b[j]=b[i];                    strcpy(a[i],temp2);                    b[i]=temp3;                }            }        printf("%d\n",k);        for(i=0;i<k;i++)        {            printf("%s %d\n",a[i],b[i]);        }    }    return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.