#include <iostream>
#include <set>
#include <algorithm>
using namespace std;
int order[26];
struct lexcmp
{
bool operator()(const string &s1,const string &s2)
{
int i=0;
int j=min(s1.size(),s2.size());
while(1)
{
if(order[s1[i]-'a']<order[s2[i]-'a'])
return true;
if(order[s1[i]-'a']>order[s2[i]-'a'])
return false;
if(i==j-1)
return false;
i++;
}
}
};
int main()
{
string s;
cin>>s;
for(int i=0;i<s.size();i++)
{
order[s[i]-'a']=i;
}
set<string,lexcmp> store;
int m;
cin>>m;
while(m--)
{
string q;
cin>>q;
store.insert(q);
}
for(auto i=store.begin();i!=store.end();i++)
{
cout<<*i<<endl;
}
}
return 0;
}
Problem in making the Custom Functor
The problem is, i have a new order of elements (instead of simple a-z). //Saved in order array
All i want is order the given strings on the based of new order.
for eg: Order is : bacdefghijklmnopqrstuvwxyz
So if the strings are ss , aa , bb
The new ordering will be bb,aa,ss.
The Code is working fine but it is giving me a problem while the strings are like "pas" "p" to be compared.
p should come before pas but it is coming after.
What modifications should i do in the functor?
Here's one approach:
#include <cassert>
#include <cstddef>
#include <cstdint>
#include <algorithm>
#include <numeric>
#include <array>
#include <string>
#include <locale>
struct lexcmp {
lexcmp() { std::iota(order_.begin(), order_.end(), std::int_fast8_t{}); }
explicit lexcmp(std::string const& order) {
assert(order.size() == order_.size());
for (std::size_t i{}; i != order_.size(); ++i) {
char const order_letter = order[i];
assert(std::isalpha(order_letter, std::locale::classic()));
assert(std::islower(order_letter, std::locale::classic()));
order_[i] = order_letter - 'a';
}
auto unique_order_letters = [this]{
auto order = order_;
std::sort(order.begin(), order.end());
return order.end() - std::unique(order.begin(), order.end()) == 0;
};
assert(unique_order_letters());
}
bool operator ()(std::string const& a, std::string const& b) const {
auto const a_len = a.size(), b_len = b.size();
std::size_t i{};
for (auto const len = std::min(a_len, b_len); i != len; ++i) {
if (auto const diff = order_[a[i] - 'a'] - order_[b[i] - 'a']) {
return diff < 0;
}
}
return i == a_len && i != b_len;
}
private:
std::array<std::int_fast8_t, 26> order_;
};
Online Demo
The following code is my solution for the Largest Number. However it will crash.
If I directly compare tampa and tempb in cmp() by using tempa > tempb instead of strcmp(), it is OK. So what is wrong here?
#include <iostream>
#include <string>
#include <stack>
#include <vector>
#include <climits>
#include <cstdio>
#include <algorithm>
#include <sstream>
#include <cstring>
using namespace std;
bool cmp(string a, string b) {
string tempa = a + b;
string tempb = b + a;
int res = strcmp(tempa.c_str(), tempb.c_str());
if (res < 0) {
return false;
} else return true;
}
class Solution {
public:
string largestNumber(vector<int>& nums) {
vector<string> str;
string res = "0";
if (nums.empty()) {
return res;
}
for (int i = 0; i < nums.size(); ++i) {
stringstream ss;
ss << nums[i];
str.push_back(ss.str());
}
sort(str.begin(), str.end(), cmp);
res = "";
for (int i = 0; i < str.size(); ++i) {
res += str[i];
}
return res[0] == '0' ? "0" : res;
}
};
int main()
{
Solution sol;
int data[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
vector<int> nums(data, data + sizeof(data) / sizeof(data[0]));
string res = sol.largestNumber(nums);
cout << res << endl;
return 0;
}
Your comparison is not equivalent to tempa > tempb. It is equivalent to !(tempa < tempb) or tempa >= tempb. And a "greater than or equal" comparison does not satisfy the requirements of std::sort. Specifically, the comparison needs to be irreflexive, which is to say that for an operand A, cmp(A, A) should be false, but with >=, it is true.
If you want the strcmp equivalent of tempa > tempb, then do:
return strcmp(tempa.c_str(), tempb.c_str()) > 0;
On closer inspection, your comparison is fundamentally broken. Why are you concatenating a and b together to form temporary strings for comparison? As one simple example of what can go wrong there, an empty string will compare equal to every other string, because:
(string("anything") + "") == (string("") + "anything")
I have a list of files :
foo10.tif
foo2.tif
...
foo102.tif
I would like to sort them according to number at end of file name.
Preferably using c++11 and lambdas.
Here is something that works. Let me know if you have any issues with it:
#include <vector>
#include <string>
#include <algorithm>
#include <iostream>
using namespace std;
int main()
{
vector<string> aFiles =
{
"foo10.tif",
"foo2.tif",
"foo102.tif",
};
sort(begin(aFiles), end(aFiles), [](const string& a, const string& b)
{
auto RemoveExtension = [](const string& s) -> string
{
string::size_type pos = s.find_last_of('.');
if (pos != string::npos)
return s.substr(0, pos);
return s;
};
string partA = RemoveExtension(a), partB = RemoveExtension(b);
if (partA.length() != partB.length())
return partA.length() < partB.length();
return partA < partB;
} );
for (string s : aFiles)
cout << s << endl;
}
I'm doing some practice questions from the book "Cracking the coding interview" and wanted to get some people to review my code for bugs and optimizations. Any feedback would be greatly appreciated.
Question: Write a method to decide if two strings are anagrams or not.
/*
Time complexity: O(n^2)
Space complexity: O(n)
*/
bool IsAnagram(std::string str1, std::string str2)
{
if(str1.length() != str2.length())
return false;
for(int i = 0; i < str1.length();i++)
{
bool found = false;
int j = 0;
while(!found && j < str2.length())
{
if(str1[i] == str2[j])
{
found = true;
str2[j] = NULL;
}
j++;
}
if(!found)
return false;
}
return true;
}
This is more efficient generally
#include <iostream>
#include <string>
#include <algorithm>
bool IsAnagram(std::string& str1, std::string& str2)
{
if(str1.length() != str2.length())
return false;
std::sort(str1.begin(), str1.end());
std::sort(str2.begin(), str2.end());
return str1.compare(str2) == 0;
}
int main(int argc, char* argv[])
{
std::string an1("army");
std::string an2("mary");
if(IsAnagram(an1, an2))
std::cout << "Hooray!\n";
return 0;
}
For those who dislike the mutating strings then maybe this is a better option. Could either remove reference to parameters 1 and 2 or make a copy inside function as here. This way, parameters can be const.
bool IsAnagram2(const std::string& str1, const std::string& str2)
{
if(str1.length() != str2.length())
return false;
std::string cpy1(str1), cpy2(str2);
std::sort(cpy1.begin(), cpy1.end());
std::sort(cpy2.begin(), cpy2.end());
return cpy1.compare(cpy2) == 0;
}
O(n) algorithm. Instead of sorting (which is O(n lg n)), count up the character occurrences in s1 and compare it to the character occurrences in s2.
#include <string>
#include <iostream>
#include <limits>
bool IsAnagram(const std::string& s1, const std::string& s2)
{
if (s1.size() != s2.size()) {
return false;
}
int count[std::numeric_limits<char>::max() + (std::size_t)1] = {};
for (auto c : s1) {
count[c]++;
}
for (auto c : s2) {
if (!count[c]) {
return false;
}
count[c]--;
}
return true;
}
int main(int argc, char **argv)
{
std::cout << IsAnagram(argv[1], argv[2]) << std::endl;
return 0;
}
There is already standard algorithm std::is_permutation that allows to perform the task simply
#include <iostream>
#include <iomanip>
#include <string>
#include <algorithm>
int main()
{
std::string s( "aab" );
std::string t( "aba" );
std::cout << std::boolalpha
<< ( s.size() == t.size() &&
std::is_permutation( s.begin(), s.end(), t.begin() ) )
<< std::endl;
return 0;
}
The output is
true
So all ypu need is to see how the algorithm is realized.:)
If you want a separate function then it will look like
bool IsAnagram( const std::string &s1, const std::string &s2 )
{
return s1.size() == s2.size() &&
std::is_permutation( s1.begin(), s1.end(), s2.begin() );
}
To use std::sort is not a good approach because original strings will be changed or you have to pass them to the function by value.
I'm trying to learn C++ from the book Accelerated C++. When trying to compile the code below, which I believe to be an exact copy of the code in the book, I get the following error and I don't understand why. Also, I have no clue how to debug this error. Any pointers in that direction (problem-solving strategy for quickly debugging this type of error) would be much much appreciated. Thanks in advance! Here's the compiler error:
g++ main.cpp split.cpp -o main
main.cpp: In function ‘void gen_aux(const Grammar&, const string&, std::vector<std::basic_string<char> >&)’:
main.cpp:64:41: error: no match for call to ‘(const std::vector<std::vector<std::basic_string<char> > >) ()’
make: *** [all] Error 1
The line at which the error occurs is the following:
const Rule_collection& c = it->second();
Complete code listing:
#include <cstdlib>
#include <iostream>
#include <map>
#include <stdexcept>
#include <string>
#include <vector>
#include "split.h"
using std::cin;
using std::cout;
using std::domain_error;
using std::endl;
using std::istream;
using std::logic_error;
using std::map;
using std::pair;
using std::string;
using std::vector;
typedef vector<string> Rule;
typedef vector<Rule> Rule_collection;
typedef map<string, Rule_collection> Grammar;
int nrand(int n) {
if (n <= 0 || n > RAND_MAX) {
throw domain_error("Argument to nrand is out of range");
}
const int bucket_size = RAND_MAX / n;
int r;
do {
r = rand() / bucket_size;
} while (r >= n);
return r;
}
bool bracketed(const string& s) {
return s.length() > 0 && s[0] == '<' && s[s.length() - 1] == '>';
}
Grammar read_grammar(istream& in) {
Grammar g;
string line;
while (getline(in, line)) {
vector<string> words = split(line);
if (!words.empty()){
g[words[0]].push_back(Rule(words.begin() + 1, words.end()));
}
}
return g;
}
void gen_aux(const Grammar& g, const string& word, vector<string>& ret) {
if (!bracketed(word)) {
ret.push_back(word);
}
else {
Grammar::const_iterator it = g.find(word);
if (it == g.end()) {
throw logic_error("empty rule");
}
const Rule_collection& c = it->second();
const Rule& r = c[nrand(c.size())];
for (Rule::const_iterator i = r.begin(); i != r.end(); i++) {
gen_aux(g, *i, ret);
}
}
}
vector<string> gen_sentence(const Grammar& g) {
vector<string> s;
gen_aux(g, "<sentence>", s);
return s;
}
int main() {
vector<string> sentence = gen_sentence(read_grammar(cin));
if (!sentence.empty()) {
vector<string>::const_iterator it = sentence.begin();
cout << *it;
it++;
while (it != sentence.end()) {
cout << " " << *it;
it++;
}
cout << endl;
}
return 0;
}
You need it->second, with no parentheses. second is a data member of an std::pair, not a member function.
const Rule_collection& c = it->second;
I guess this simply has to be "const Rule_collection& c = it->second;"
Otherwise you would try to call the vector like a function.