我试图找到没有重复字符的最长子字符串.我有一个布尔矢量来跟踪256个ascii字符.
#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
string s = "aaaaaaaaaaaaasadfrhtytbgrbrsvsrhvsg";
vector<bool> v(256, false);
int j = 0, len = 0, index = 0;
for(int i = 0; i < s.length(); i++)
{
if(!v[s[i]])
{
j++;
if(j > len)
{
len = j;
index = i - j;
}
v[s[i]] = true;
}
else
{
j = 0;
v.clear();
}
}
cout << s.substr(index, len) + " " << len << endl; …Run Code Online (Sandbox Code Playgroud) 我有一个简单的程序.
#include <cstdio>
int main()
{
int num = 000012345; printf("%d\n",num);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
上面的程序给出了5349.为什么?我的意思是它应该是错的,但为什么5349?
我正在尝试使用 C++ 中的向量向量作为二维数组。我必须读取用户的输入。行数和列数也由用户输入。
假设我读取 m * n 矩阵,
我尝试使用 Reserve(m*n) 分配空间,但在构建时也出现错误。
这是我面临的一个普遍问题,我的意思是即使在逐个字符读取的字符串中,如何为其提供空间以便可以访问索引 i (我知道可以使用 .resize(given_size) 来完成),但在以下情况下不知道用户将输入多少个字符,这是无法完成的(可以使用 + 运算符来规避,但这仍然不是直接的解决方案)。
所以,我的主要问题是如何初始化向量向量(不放入值,而只是分配所需的空间、行和列),以便我可以访问 [i][j] 来读取值?
我知道可以像这里一样使用向量的向量来构建矩阵,但我想首先声明一个向量的向量,然后获取行和列的输入来分配空间,以便我可以访问 [i][j] 输入元素。
我试图找到最长的子串,最多2个不同的字符.这是一个蛮力程序,只使用所有可能的子串并检查它们是否有2个或更多不同的字符.
我用一套来跟踪不同的字符.
#include <iostream>
#include <string>
#include <algorithm>
#include <unordered_set>
using namespace std;
int main()
{
string s = "AllPossibleSubstrings";
int l=0,index=0;
for(int i =0;i<s.length();i++)
{
for(int j=i+1;j<s.length();j++)
{
string sub = string(s.begin()+i,s.begin()+j);
unordered_set<char> v;
for(auto x:sub)
{
v.insert(x);
}
if(v.size()<=2) {l=max(l,j-i+1); if(l==j-i+1) index=i;}
}
}
cout<<l<<" "+s.substr(index,l)<<endl;
}
Run Code Online (Sandbox Code Playgroud)
我得到了错误的答案4 ssib,而正确的答案一定不能有b(All,llP,oss,ssi都是可能的答案).我哪里做错了?
我无法弄清楚为什么我会收到非法的间接错误:
#include <iostream>
#include <algorithm>
#include <stack>
#include <unordered_map>
using namespace std;
int main()
{
string s1 = "";
stack<char> s;
unordered_map<char, char> m;
m.insert('(', ')');
m.insert('{', '}');
m.insert('[', ']');
for (auto x : s1)
{
if (m.find(x) != m.end()) s.push(x);
else
{
auto it = m.find(s.top());
if (s.empty() || (it->second) != x) { cout << "Invalid\n"; s.pop(); break; }
}
}
}
Run Code Online (Sandbox Code Playgroud)
它是映射条目的迭代器,我试图访问它的值.
我知道数组可以通过很多方式传递给函数.
#include <iostream>
#include <utility>
using namespace std;
pair<int, int> problem1(int a[]);
int main()
{
int a[] = { 10, 7, 3, 5, 8, 2, 9 };
pair<int, int> p = problem1(a);
cout << "Max =" << p.first << endl;
cout << "Min =" << p.second << endl;
getchar();
return 0;
}
pair<int,int> problem1(int a[])
{
int max = a[0], min = a[0], n = sizeof(a) / sizeof(int);
for (int i = 1; i < n; i++)
{
if (a[i]>max) …Run Code Online (Sandbox Code Playgroud) 我正在寻找对二进制字符串的一些 STL 支持。bitset似乎非常有用,但是我无法成功地操作各个位。
#include <iostream>
#include <bitset>
using namespace std;
int main()
{
string b = bitset<8>(128).to_string();
for(auto &x:b)
{
x = 1 and x-'0' ; cout<<b<<"\n";
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
那么,我应该使用 vector 还是 bitset 来操作单个位?
上面的程序给出:
?0000000
? 000000
? 00000
? 0000
? 000
? 00
? 0
?
Run Code Online (Sandbox Code Playgroud)
我知道发生这种情况是因为我正在操作 char,当设置为 0 时会打印关联的 ascii 字符。我的问题是我可以遍历一个位集并同时修改单个位吗?
例如我肯定不能在下面做:
#include <iostream>
#include <string>
#include <bitset>
int main ()
{
std::bitset<16> baz (std::string("0101111001"));
std::cout << "baz: " << baz << …Run Code Online (Sandbox Code Playgroud)