Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <utility>
- #include <tuple>
- #include <vector>
- #include <stack>
- #include <algorithm>
- #include <iterator>
- using namespace std;
- int numfun (string s){
- int accu=0;
- for (auto c : s){
- accu+= toupper(c)-'A'+1;
- }
- return accu;
- }
- struct szperacz_kombinatoryczny{
- vector <string> wyrazy;
- vector <int> wartosci;
- int cel;
- int max_l;
- szperacz_kombinatoryczny(int cel_, int max_l_);
- void wypluj();
- vector<int > St; //stos na indeksu wyrazów
- void iter(int j, int sum , int level);
- };
- void szperacz_kombinatoryczny::iter(int j, int sum, int level){
- if (sum==cel){
- wypluj();
- return;
- }
- if (level>=max_l) return;
- for (int i=j; i<wartosci.size();i++){
- if (sum+wartosci[i]>cel) continue;
- St.push_back(i);
- iter(i+1,sum+wartosci[i], level+1);
- St.pop_back();
- }
- }
- void szperacz_kombinatoryczny::wypluj(){
- for (auto i: St){
- cout<<wyrazy[i]<<" ";
- }
- cout<<"\n";
- }
- szperacz_kombinatoryczny::szperacz_kombinatoryczny(int cel_, int max_l_){
- string s;
- cel = cel_;
- max_l = max_l_;
- while (cin){
- cin>>s;
- wyrazy.push_back(s);
- }
- sort(begin(wyrazy), end(wyrazy));
- wyrazy.erase( unique(begin(wyrazy), end(wyrazy)), end(wyrazy) );
- cout<<"załadowano "<<wyrazy.size()<<" unikalnych wyrazów"<<endl;
- transform( begin(wyrazy), end(wyrazy), back_inserter(wartosci), [](const string &s){ return numfun(s);} );
- }
- int main(int argc, char *argv[])
- {
- szperacz_kombinatoryczny sp(666,5);
- sp.iter(0,0,0);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement