设为首页 加入收藏

TOP

hdu 2072 (一)
2014-11-23 20:25:35 来源: 作者: 【 】 浏览:9
Tags: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
#include
#include
#include

using namespace std;

set 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< }

return 0;
}

/*
* 2072_2.cpp
*
* Created on: 2013年8月7日
* Author: Administrator
* 总有一天我会追到章泽天的。。。。
*/

#include
#include
#include
#include

using namespace std;

set 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< }

return 0;
}


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


[plain]
#include
#include

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 temp[i-start] == '\0';
for (int j=0; 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
#include

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 temp[i-start] == '\0';
for (int j=0; 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的代码:


[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 ;

首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇hdu 4649 Professor Tian 多校联.. 下一篇hdu1166 树状数组

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·MySQL 基础入门视频 (2025-12-26 23:20:22)
·小白入门:MySQL超详 (2025-12-26 23:20:19)
·关于 MySQL 数据库学 (2025-12-26 23:20:16)
·SOLVED: Ubuntu 24.0 (2025-12-26 22:51:53)
·Linux 常用命令最全 (2025-12-26 22:51:50)