当前位置:编程学习 > C/C++ >>

hdu 2072

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

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

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

c++(使用STL来做)

 

 

[cpp] 
/*
 * 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; 

/*
 * 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版本(小敬大神奉献的)


[plain]
#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; 

#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;
 &n

补充:软件开发 , C++ ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,