这道题是个简单题。但是我犯了个可能很常见的bug,然后用了很久才调试出来。我猜打印出中间过程,并验证是否符合预期,很快就能调出来。但我是通过测试数据+别人的正确程序中间结果(每段的size)对比出来的。。。
测试数据:
输入:http://www.informatik.uni-ulm.de/acm/Locals/2005/input/bingo.in
输出:http://www.informatik.uni-ulm.de/acm/Locals/2005/output/bingo.out
不用谢。
正确程序:http://www.cnblogs.com/lidaojian/archive/2012/04/19/2457448.html
后来发现错误的原因很简单。对每行处理完后,我没有考虑最后那个单词。于是for循环结束后,word里面有个未处理的单词。解决的办法是再判断是不是到行末了,如果是行末的话也处理下。
thestoryofsnow | 2471 | Accepted | 212K | 32MS | C++ | 1637B |
/*
ID: thestor1
LANG: C++
TASK: poj2471
*/
#include <iostream>
#include <fstream>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <limits>
#include <string>
#include <vector>
#include <list>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <algorithm>
#include <cassert>
using namespace std;
int gcd(int a, int b)
{
if (b == 0)
{
return a;
}
return gcd(b, a % b);
}
int main()
{
// printf("gcd(21, 18): %d\n", gcd(21, 18));
// printf("gcd(12, 24): %d\n", gcd(12, 24));
string line, word;
set<string> words;
int sum = 0;
int ngames = 0;
bool isChar = true;;
while (getline(cin, line))
{
// printf("line: [%s]\n", line.c_str());
for (int i = 0; i < line.size(); ++i)
{
isChar = true;
if ('a' <= line[i] && line[i] <= 'z')
{
word.push_back(line[i]);
}
else if ('A' <= line[i] && line[i] <= 'Z')
{
word.push_back(line[i] - 'A' + 'a');
}
else
{
isChar = false;
}
if (!isChar || i == line.size() - 1)
{
if (word == "bullshit")
{
sum += words.size();
// printf("ngames: %d, words.size(): %d\n", ngames, words.size());
// cout << "words(" << words.size() << "): ";
// for (set<string>::iterator iter = words.begin(); iter != words.end(); ++iter)
// {
// cout << "[" << *iter << "] ";
// }
// cout << endl;
ngames++;
words.clear();
}
else if (word.size() > 0)
{
words.insert(word);
}
word.clear();
}
}
}
// printf("sum: %d, ngames: %d\n", sum, ngames);
int g = gcd(sum, ngames);
printf("%d / %d\n", sum / g, ngames / g);
return 0;
}