hdu 2072

来源:互联网 发布:湖南软件职业学院学费 编辑:程序博客网 时间:2024/05/13 00:48

这道题用java来做是很快的,可是不知道今天为什么,用java来做就是ac不了,所以就改用c、c++来做了

解题思路:这一道题其实主要的问题就是解决空格问题以及单词的重复问题。用stringstream来解决空格问题,

用set来解决残次的重复问题,这道题也就迎刃而解了。以下附上c和c++的ac代码.(我的另一片文章中有从网上转载的大神的streamstring的一些资料)

c++(使用STL来做)


/* * 2072_2.cpp * *  Created on: 2013年8月7日 *      Author: Administrator *      总有一天我会追到章泽天的。。。。 */#include <iostream>#include <string>#include <set>#include<sstream>using namespace std;set<string> words;int main(){string row,input;//getline(cin,row) 用row来保存一行的内容while(getline(cin,row)&&row!="#"){words.clear();//定义一个stringstream类型的对象stringstream str(row);//str >> input 给input赋值while(str >> input){words.insert(input);}cout<<words.size()<<endl;}return 0;}


c版本(小敬大神奉献的)

#include<cstdio>#include<cstring>char ch[110000], a[1100][110], temp[110];int main(){    while (1) {        gets(ch);        if (strcmp(ch, "#") == 0) break;        int num = 0, len = strlen(ch);        int start = 0, end;        while (1) {            while (start < len && ch[start] == ' ') ++start;            end = start + 1;            while (end < len && ch[end] != ' ') ++end;            if (start < len) {                int has = 0;                int i;                for (i=start; i<end; ++i) temp[i-start] = ch[i];                temp[i-start] == '\0';                for (int j=0; j<num; ++j) {                    if (strcmp(a[j], temp) == 0) {                        has = 1;                        break;                    }                }                if (!has) {                    strcpy(a[num], temp);                    ++num;                }                for (int j=0; j<110; ++j) temp[j] = '\0';            }            else break;            start = end + 1;        }        printf("%d\n", num);    }    return 0;}


以下贴一下没有ac的java的代码:

package com.njupt.acm;import java.util.HashMap;import java.util.Map;import java.util.Scanner;public class HDU_2072 {public static void main(String[] args) {Scanner scanner = new Scanner(System.in);String str ;Map map;while(true){str = scanner.nextLine();str =  str.trim().replaceAll("\\s{1,}", " ");if(str.startsWith("#")){break;}String[] strs = str.split(" ");map = new HashMap();for(int i = 0 ; i < strs.length ; ++i){map.put(strs[i], 0);}System.out.println(map.keySet().size());}}}



原创粉丝点击