如何从印度计算奥林匹克运动会问题解决这个问题? [英] How do I solve this problem from indian computing olympiad problem archieve?
本文介绍了如何从印度计算奥林匹克运动会问题解决这个问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是一个问题: IARCS问题档案 [ ^ ]
我尝试了什么:
我编写的代码如下,虽然据我所知,它在逻辑上和语法上都是正确的,它给出了运行时错误。欢迎提出任何建议和提示。
This is the question : IARCS Problem Archive[^]
What I have tried:
I've written the code as follows, although up to my knowledge, it is logically and syntactically correct, it gives a runtime error. Any suggestions and tips are welcome.
#include<bits stdc++.h="">
using namespace std;
#define c temp[j]
int main()
{
ios::sync_with_stdio(false);
vector<string> words;
string temp,uni;
int N,i,j,start,no=0;
//Input and Computation
cin >> N;
for(i = 0;i < N;i++)
{
getline(cin,temp);//Getting the line of text
start = 0;
for(j = 0;j < (int)temp.length();j++)//Extracting words and storing in vector
{
if(c >= 65 && c <= 90)
c = c + 32;
if(c == ' '||c == '.'||c == ';'||c == ','||c == ':')
{
uni = temp.substr(start,j-start-1);
words.push_back(uni);
no++;
start = j;
}
}
temp = "";
}
sort(words.begin(),words.end());
//Output
cout << no;
for(i = 0;i < no-1;i++)//Removing duplicates
{
if(words[i] != words[i+1])
cout << words[i] <<'\n';
}
cout << words[no-1];
return 0;
}
推荐答案
uni = temp.substr(start,j-start-1);
看起来像是一个潜在的界限。
如果j == 0,这将是
looks like a potential out-of-bounds.
if j==0, this will be
temp.substr(0, -1);
编译并运行代码,并给它这是一行作为输入,代码断言
compiling and running the code, and giving it "this is a line" as input, the code asserts on
cout << words[no-1];
因为单词是空的。
所以,显然它不是逻辑和语法正确。
because words is empty.
so, apparently it is not "logically and syntactically correct".
这篇关于如何从印度计算奥林匹克运动会问题解决这个问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文