hdu 2072

149 篇文章 0 订阅
146 篇文章 1 订阅

这道题用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());
		}
		
		
		
	}
}



  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

帅气的东哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值