File tree Expand file tree Collapse file tree 5 files changed +256
-0
lines changed
Expand file tree Collapse file tree 5 files changed +256
-0
lines changed Original file line number Diff line number Diff line change 1+ #include < algorithm>
2+ #include < iostream>
3+ #include < sstream>
4+ #include < string>
5+ #include < vector>
6+ #include < queue>
7+ #include < set>
8+ #include < map>
9+ #include < cstdio>
10+ #include < cstdlib>
11+ #include < cctype>
12+ #include < cmath>
13+ #include < string>
14+ #include < cstring>
15+ using namespace std ;
16+
17+ #define REP (i,n ) for (int i=0 ;i<(n);++i)
18+ #define FOR (i,a,b ) for (int i=(a);i<=(b);++i)
19+ #define RFOR (i,a,b ) for (int i=(a);i>=(b);--i)
20+ #define FOREACH (it,c ) for (typeof ((c).begin())it=(c).begin();it!=(c).end();++it)
21+ #define CLR (x ) memset((x),0 ,sizeof ((x)))
22+ #define MP make_pair
23+ #define MPI make_pair<int , int >
24+ #define PB push_back
25+ typedef long long LL;
26+ typedef vector<int > VI;
27+ typedef vector<string> VS;
28+ typedef pair<int , int > PI;
29+
30+ class Solution {
31+ public:
32+ vector<string> anagrams (vector<string> &strs) {
33+ vector<string> res;
34+ int n = strs.size ();
35+ map<string, int > mp;
36+ REP (i,n) {
37+ string s = strs[i];
38+ sort (s.begin (), s.end ());
39+ if (mp.find (s) == mp.end ()) mp[s] = i;
40+ else {
41+ if (mp[s] >= 0 ) res.PB (strs[mp[s]]);
42+ res.PB (strs[i]);
43+ mp[s] = -1 ;
44+ }
45+ }
46+ return res;
47+ }
48+ };
49+
50+ int main () {
51+ Solution s = Solution ();
52+ return 0 ;
53+ }
Original file line number Diff line number Diff line change 1+ #include < algorithm>
2+ #include < iostream>
3+ #include < sstream>
4+ #include < string>
5+ #include < vector>
6+ #include < queue>
7+ #include < set>
8+ #include < map>
9+ #include < cstdio>
10+ #include < cstdlib>
11+ #include < cctype>
12+ #include < cmath>
13+ #include < string>
14+ #include < cstring>
15+ using namespace std ;
16+
17+ #define REP (i,n ) for (int i=0 ;i<(n);++i)
18+ #define FOR (i,a,b ) for (int i=(a);i<=(b);++i)
19+ #define RFOR (i,a,b ) for (int i=(a);i>=(b);--i)
20+ #define FOREACH (it,c ) for (typeof ((c).begin())it=(c).begin();it!=(c).end();++it)
21+ #define CLR (x ) memset((x),0 ,sizeof ((x)))
22+ #define MP make_pair
23+ #define MPI make_pair<int , int >
24+ #define PB push_back
25+ typedef long long LL;
26+ typedef vector<int > VI;
27+ typedef vector<string> VS;
28+ typedef pair<int , int > PI;
29+
30+ class Solution {
31+ public:
32+ vector<VI> res;
33+ VI mm;
34+ void doit (int idx, int n, int k) {
35+ if (idx == k) {
36+ res.PB (mm);
37+ } else {
38+ int st;
39+ if (idx == 0 ) st = 1 ;
40+ else st = mm[idx - 1 ] + 1 ;
41+ FOR (i,st,n) {
42+ mm[idx] = i;
43+ doit (idx + 1 , n, k);
44+ }
45+ }
46+ }
47+ vector<vector<int > > combine (int n, int k) {
48+ res.clear ();
49+ mm.resize (k);
50+ doit (0 , n, k);
51+ return res;
52+ }
53+ };
54+
55+ int main () {
56+ Solution s = Solution ();
57+ vector<VI> mm = s.combine (4 , 2 );
58+ REP (i,mm.size ()) {
59+ REP (j,mm[i].size ()) cout << mm[i][j] << " " ;
60+ cout << endl;
61+ }
62+ return 0 ;
63+ }
Original file line number Diff line number Diff line change 1+ class Solution {
2+ public:
3+ double doit (double x, int n) {
4+ if (n == 0 ) return 1 ;
5+ double t = pow (x, n / 2 );
6+ if (n & 1 ) return t * t * x;
7+ else return t * t;
8+ }
9+ double pow (double x, int n) {
10+ if (n < 0 ) return 1.0 / doit (x, -n);
11+ else return doit (x, n);
12+ }
13+ };
Original file line number Diff line number Diff line change 1+ #include < algorithm>
2+ #include < iostream>
3+ #include < sstream>
4+ #include < string>
5+ #include < vector>
6+ #include < queue>
7+ #include < set>
8+ #include < map>
9+ #include < cstdio>
10+ #include < cstdlib>
11+ #include < cctype>
12+ #include < cmath>
13+ #include < string>
14+ #include < cstring>
15+ using namespace std ;
16+
17+ #define REP (i,n ) for (int i=0 ;i<(n);++i)
18+ #define FOR (i,a,b ) for (int i=(a);i<=(b);++i)
19+ #define RFOR (i,a,b ) for (int i=(a);i>=(b);--i)
20+ #define FOREACH (it,c ) for (typeof ((c).begin())it=(c).begin();it!=(c).end();++it)
21+ #define CLR (x ) memset((x),0 ,sizeof ((x)))
22+ #define MP make_pair
23+ #define MPI make_pair<int , int >
24+ #define PB push_back
25+ typedef long long LL;
26+ typedef vector<int > VI;
27+ typedef vector<string> VS;
28+ typedef pair<int , int > PI;
29+
30+ class Solution {
31+ public:
32+ vector<VS> res;
33+ bool isok (VI& mm, int row) {
34+ REP (i,row) {
35+ if (mm[i] == mm[row] || (abs (mm[i] - mm[row]) == (row - i))) return false ;
36+ }
37+ return true ;
38+ }
39+ void doit (VI& mm, int row, int n) {
40+ if (row == n) {
41+ VS t;
42+ REP (i,n) {
43+ string s (n, ' .' );
44+ s[mm[i]] = ' Q' ;
45+ t.PB (s);
46+ }
47+ res.PB (t);
48+ } else {
49+ REP (i,n) {
50+ mm[row] = i;
51+ if (isok (mm, row)) doit (mm, row + 1 , n);
52+ }
53+ }
54+ }
55+ vector<vector<string> > solveNQueens (int n) {
56+ res.clear ();
57+ VI mm (n, -1 );
58+ doit (mm, 0 , n);
59+ return res;
60+ }
61+ };
62+
63+ int main () {
64+ Solution s = Solution ();
65+ return 0 ;
66+ }
Original file line number Diff line number Diff line change 1+ #include < algorithm>
2+ #include < iostream>
3+ #include < sstream>
4+ #include < string>
5+ #include < vector>
6+ #include < queue>
7+ #include < set>
8+ #include < map>
9+ #include < cstdio>
10+ #include < cstdlib>
11+ #include < cctype>
12+ #include < cmath>
13+ #include < string>
14+ #include < cstring>
15+ using namespace std ;
16+
17+ #define REP (i,n ) for (int i=0 ;i<(n);++i)
18+ #define FOR (i,a,b ) for (int i=(a);i<=(b);++i)
19+ #define RFOR (i,a,b ) for (int i=(a);i>=(b);--i)
20+ #define FOREACH (it,c ) for (typeof ((c).begin())it=(c).begin();it!=(c).end();++it)
21+ #define CLR (x ) memset((x),0 ,sizeof ((x)))
22+ #define MP make_pair
23+ #define MPI make_pair<int , int >
24+ #define PB push_back
25+ typedef long long LL;
26+ typedef vector<int > VI;
27+ typedef vector<string> VS;
28+ typedef pair<int , int > PI;
29+
30+ class Solution {
31+ public:
32+ int res;
33+ bool isok (int mm[], int row) {
34+ REP (i,row) {
35+ if (mm[i] == mm[row] || (abs (mm[i] - mm[row]) == (row - i))) return false ;
36+ }
37+ return true ;
38+ }
39+ void doit (int mm[], int row, int n) {
40+ if (row == n) {
41+ ++res;
42+ } else {
43+ REP (i,n) {
44+ mm[row] = i;
45+ if (isok (mm, row)) doit (mm, row + 1 , n);
46+ }
47+ }
48+ }
49+
50+ int totalNQueens (int n) {
51+ res = 0 ;
52+ int *mm = new int [n];
53+ doit (mm, 0 , n);
54+ return res;
55+ }
56+ };
57+
58+ int main () {
59+ Solution s = Solution ();
60+ return 0 ;
61+ }
You can’t perform that action at this time.
0 commit comments