Friday, April 24, 2015

UVa - 10293 - Word Length and Frequency

#include <iostream>
#include <cstring>
#include <string>
#include <cstdio>
using namespace std;

int main(){
    string text;
    int cnt[1000], cn = 0, hyp;memset(cnt, 0, sizeof(cnt));
    while(getline(cin, text)){
        if(text == "#"){
            for(int i = 0; i < 1000; i++)if(cnt[i] > 0)cout << i << " " << cnt[i] << endl;
            memset(cnt, 0, sizeof(cnt)), cn = 0;
            cout << endl;
        }
        else{
            int sz = text.size();
            for(int i = 0; i < sz; i++){
                if(text[i] == '-')hyp = 1;
                else if((text[i] >= 'A' && text[i] <= 'Z') || (text[i] >= 'a' && text[i] <= 'z'))cn++, hyp = 0;
                else if(text[i] == '\'' || text[i] == '-')cn *= 1;
                else{
                    if(cn > 0 && hyp == 0)cnt[cn]++, cn = 0;
                }
                if(hyp == 0 && cn > 0 && i == sz-1)cnt[cn]++, cn = 0;
            }
        }
    }
    return 0;
}

No comments:

Post a Comment