10815 - Andy's First Dictionary

All about problems in Volume CVIII. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

Re: 10815 - Andy's First Dictionary

Postby AbdoAdel » Mon Aug 22, 2011 12:29 pm

Hi, this is my first post here :)
im getting a WA
this is my code :
Code: Select all
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <string>
#include <cstring>
#include <cctype>
#include <cmath>
#include <vector>
#include <stack>
#include <map>
#include <set>
#include <algorithm>
#include <iterator>

using namespace std;

void parseLine(const string& line, set<string>& words);
string removePunctAndConvertToLowerCase(const string& word);

int main() {
   string line;
   set<string> words;
   while (getline(cin, line))
      if (line != "")
         parseLine(line, words);
   for (set<string>::iterator it = words.begin(); it != words.end(); it++)
      cout << (*it) << endl;
   return 0;
}

void parseLine(const string& line, set<string>& words) {
   stringstream ss(stringstream::in | stringstream::out);
   ss << line;
   while (!ss.eof()) {
      string word;
      ss >> word;
      word = removePunctAndConvertToLowerCase(word);
      if (word != "" && words.find(word) == words.end())
         words.insert(word);
   }
}

string removePunctAndConvertToLowerCase(const string& word) {
   string tmp;
   for (int i = 0; i < (int) word.length(); i++) {
      if (isalpha(word[i]))
         tmp += tolower(word[i]);
   }
   return tmp;
}

General idea: i read a string containing one line, if that line is not empty, then ( parseLine ) parses it to words using string streams, removes punctuation and converts to lower case form and then check if it is not listed before
I would be very thankful if some one helped :wink:
AbdoAdel
New poster
 
Posts: 1
Joined: Mon Aug 22, 2011 12:22 pm

Re: 10815 - Andy's First Dictionary

Postby sith » Tue Jun 19, 2012 5:01 pm

Hello

Why my solution is wrong?

Code: Select all
class Main {
    public static void main(String[] args) {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        String line;
        try {
            Set<String> result = new HashSet<String>();
            while ((line = reader.readLine()) != null) {

                StringTokenizer tokenizer = new StringTokenizer(line, " !\"#$%&'()*+,-./0123456789:;<=>?@[]^_`{|}~");
                while (tokenizer.hasMoreTokens()) {
                    result.add(tokenizer.nextToken().toLowerCase());
                }
            }
            String[] words = result.toArray(new String[result.size()]);
            Arrays.sort(words);

            for (String word : words) {
                System.out.println(word);
            }
        }
        catch (IOException e) {

        }
    }
}
sith
Learning poster
 
Posts: 67
Joined: Sat May 19, 2012 7:46 pm

Re: 10815 - Andy's First Dictionary

Postby brianfry713 » Tue Jun 19, 2012 11:30 pm

This code doesn't compile. Post the full code with the imports.
brianfry713
Guru
 
Posts: 1761
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 10815 - Andy's First Dictionary

Postby sith » Wed Jun 20, 2012 11:06 am

Sorry. Here is the full code

Code: Select all
AC
Last edited by sith on Thu Jun 21, 2012 12:40 pm, edited 1 time in total.
sith
Learning poster
 
Posts: 67
Joined: Sat May 19, 2012 7:46 pm

Re: 10815 - Andy's First Dictionary

Postby brianfry713 » Wed Jun 20, 2012 10:40 pm

For an input file containing:
Code: Select all
a\bc
My AC output is:
Code: Select all
a
bc
I used the c function isalpha().
brianfry713
Guru
 
Posts: 1761
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 10815 - Andy's First Dictionary

Postby sith » Thu Jun 21, 2012 12:40 pm

Thanks, it works
sith
Learning poster
 
Posts: 67
Joined: Sat May 19, 2012 7:46 pm

Previous

Return to Volume CVIII

Who is online

Users browsing this forum: No registered users and 0 guests