Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 234bc96

Browse files
some practise
1 parent 69da45a commit 234bc96

File tree

8 files changed

+420
-77
lines changed

8 files changed

+420
-77
lines changed

‎DSAPractice/graph/tbt.cpp‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
2+
#include <bits/stdc++.h>
3+
using namespace std;
4+
int main(){
5+
ios::sync_with_stdio(false);
6+
cin.tie(0);cout.tie(0);
7+
8+
int t;
9+
cin>>t;
10+
11+
while(t--){
12+
int n;
13+
cin>>n;
14+
15+
string s[3*n+1];
16+
map<string,int>mp;
17+
18+
for(int i=0;i<3*n;i++){
19+
cin>>s[i];
20+
mp[s[i]]++;
21+
}
22+
23+
int c=0;
24+
for(int i=0;i<3*n;i++){
25+
26+
if(mp[s[i]]==1) c+=3;
27+
if(mp[s[i]]==2) c+=1;
28+
29+
if(i%n==n-1)
30+
{
31+
cout<<c<<" ";c=0;
32+
}
33+
}
34+
cout<<endl;
35+
}
36+
37+
}
Lines changed: 42 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,105 +1,86 @@
11
import java.io.*;
22
import java.util.*;
3+
34
//https://leetcode.com/problems/permutation-sequence/
45
public class PermutationSequence {
56
static BufferedReader br;
67
static StringTokenizer st;
78
PermutationSequence ps = new PermutationSequence();
89

9-
static int nextInt()throws IOException
10-
{
10+
static int nextInt() throws IOException {
1111
return Integer.parseInt(next());
1212
}
1313

14-
static String next()throws IOException
15-
{
16-
while(st == null || !st.hasMoreTokens())
17-
{
18-
st=new StringTokenizer(br.readLine());
14+
static String next() throws IOException {
15+
while (st == null || !st.hasMoreTokens()) {
16+
st = new StringTokenizer(br.readLine());
1917
}
2018
return st.nextToken();
2119
}
2220

23-
public static void main(String[] args)throws IOException
24-
{
21+
public static void main(String[] args) throws IOException {
2522
br = new BufferedReader(new InputStreamReader(System.in));
26-
23+
2724
int n = nextInt();
2825
int k = nextInt();
29-
System.out.println(getPermutation(n, k));
26+
System.out.println(getPermutation(n, k));
3027
}
3128

3229
public static String getPermutation(int n, int k) {
3330
List<Integer> nums = new ArrayList<>();
34-
for(int i = 0;i<n;i++)
35-
{
36-
nums.add(i+1);
31+
for (int i = 0; i < n; i++) {
32+
nums.add(i + 1);
3733
}
3834
List<List<Integer>> ans = new ArrayList<>();
39-
permute(nums,ans,0,n-1);
40-
41-
42-
Collections.sort(ans,new Comparator<List<Integer>>(){
43-
35+
permute(nums, ans, 0, n - 1);
36+
37+
Collections.sort(ans, new Comparator<List<Integer>>() {
38+
4439
@Override
45-
public int compare(List<Integer> val1, List<Integer> val2)
46-
{
47-
if(val1.get(0)>val2.get(0))
48-
{
40+
public int compare(List<Integer> val1, List<Integer> val2) {
41+
if (val1.get(0) > val2.get(0)) {
4942
return 1;
50-
}
51-
else
52-
{
43+
} else {
5344
return -1;
5445
}
5546
}
5647
});
57-
58-
59-
48+
6049
System.out.println(ans);
6150
StringBuilder sb = new StringBuilder();
62-
nums=ans.get(k-1);
51+
nums = ans.get(k - 1);
6352
int i = 0;
64-
while (i < nums.size() - 1)
65-
{
53+
while (i < nums.size() - 1) {
6654
sb.append(nums.get(i));
67-
55+
6856
i++;
6957
}
7058
sb.append(nums.get(i));
71-
59+
7260
String res = sb.toString();
73-
return res;
74-
75-
61+
return res;
62+
7663
}
77-
78-
static void permute(List<Integer> nums,List<List<Integer>> ans, int l, int r)
79-
{
80-
if (l == r)
81-
ans.add(new ArrayList<>(nums));
82-
else
83-
{
84-
for (int i = l; i <= r; i++)
85-
{
86-
nums = swap(nums,l,i);
87-
permute(nums,ans, l+1, r);
88-
nums = swap(nums,l,i);
89-
}
90-
}
64+
65+
static void permute(List<Integer> nums, List<List<Integer>> ans, int l, int r) {
66+
if (l == r)
67+
ans.add(new ArrayList<>(nums));
68+
else {
69+
70+
for (int i = l; i <= r; i++) {
71+
nums = swap(nums, l, i);
72+
permute(nums, ans, l + 1, r);
73+
nums = swap(nums, l, i);
74+
}
75+
}
9176
}
92-
93-
static List<Integer> swap(List<Integer> nums, int l,int i)
94-
{
77+
78+
static List<Integer> swap(List<Integer> nums, int l, int i) {
9579
int temp;
96-
temp=nums.get(i);
97-
nums.set(i,nums.get(l));
98-
nums.set(l,temp);
80+
temp = nums.get(i);
81+
nums.set(i,nums.get(l));
82+
nums.set(l,temp);
9983
return nums;
10084
}
101-
102-
103-
104-
85+
10586
}

‎DSAPractice/recursion/PermutationSequence2.java‎

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,26 @@
11
import java.io.*;
22
import java.util.*;
3+
34
//https://leetcode.com/problems/permutation-sequence/
45
public class PermutationSequence2 {
56
static BufferedReader br;
67
static StringTokenizer st;
78
PermutationSequence ps = new PermutationSequence();
89

9-
static int nextInt()throws IOException
10-
{
10+
static int nextInt() throws IOException {
1111
return Integer.parseInt(next());
1212
}
1313

14-
static String next()throws IOException
15-
{
16-
while(st == null || !st.hasMoreTokens())
17-
{
18-
st=new StringTokenizer(br.readLine());
14+
static String next() throws IOException {
15+
while (st == null || !st.hasMoreTokens()) {
16+
st = new StringTokenizer(br.readLine());
1917
}
2018
return st.nextToken();
2119
}
2220

23-
public static void main(String[] args)throws IOException
24-
{
21+
public static void main(String[] args) throws IOException {
2522
br = new BufferedReader(new InputStreamReader(System.in));
26-
23+
2724
int n = nextInt();
2825
int k = nextInt();
2926
System.out.println(getPermutation(n, k));
@@ -56,12 +53,10 @@ static void permute(char[] nums, List<String> ans, int l, int r) {
5653

5754
static char[] swap(char[] nums, int l, int i) {
5855
char ch;
59-
ch=nums[i];
60-
nums[i]=nums[l];
61-
nums[l]=ch;
56+
ch = nums[i];
57+
nums[i] = nums[l];
58+
nums[l] = ch;
6259
return nums;
6360
}
64-
65-
66-
61+
6762
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
class combinationsum1 {
5+
static BufferedReader br;
6+
static StringTokenizer st;
7+
8+
static int nextInt() throws IOException {
9+
return Integer.parseInt(next());
10+
}
11+
12+
static String next() throws IOException {
13+
while (st == null || !st.hasMoreTokens()) {
14+
st = new StringTokenizer(br.readLine());
15+
}
16+
return st.nextToken();
17+
}
18+
19+
static long nextLong() throws IOException {
20+
return Long.parseLong(next());
21+
}
22+
23+
public static void main(String args[]) throws IOException {
24+
br = new BufferedReader(new InputStreamReader(System.in));
25+
int t = nextInt();
26+
while (t-- > 0) {
27+
input();
28+
}
29+
br.close();
30+
}
31+
32+
public static void input() throws IOException {
33+
int n = nextInt();
34+
int[] candidate = new int[n];
35+
for (int i = 0; i < n; i++) {
36+
candidate[i] = nextInt();
37+
38+
}
39+
int k = nextInt();
40+
List<List<Integer>> ans = new ArrayList<>();
41+
List<Integer> ds = new ArrayList<>();
42+
System.out.println(combination(0, n, k, candidate, ans, ds));
43+
}
44+
45+
static List<List<Integer>> combination(int index, int n, int target, int[] candidates, List<List<Integer>> ans,
46+
List<Integer> ds) {
47+
if (candidates[index] == candidates.length) {
48+
if (target == 0) {
49+
ans.add(new ArrayList<>(ds));
50+
}
51+
return ans;
52+
}
53+
54+
if (target >= candidates[index]) {
55+
ds.add(candidates[index]);
56+
combination(index, n, target - candidates[index], candidates, ans, ds);
57+
ds.remove(ds.size() - 1);
58+
}
59+
combination(index + 1, n, target, candidates, ans, ds);
60+
return ans;
61+
}
62+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
class combinationsum2 {
5+
static BufferedReader br;
6+
static StringTokenizer st;
7+
8+
static int nextInt() throws IOException {
9+
return Integer.parseInt(next());
10+
}
11+
12+
static String next() throws IOException {
13+
while (st == null || !st.hasMoreTokens()) {
14+
st = new StringTokenizer(br.readLine());
15+
}
16+
return st.nextToken();
17+
}
18+
19+
static long nextLong() throws IOException {
20+
return Long.parseLong(next());
21+
}
22+
23+
public static void main(String args[]) throws IOException {
24+
br = new BufferedReader(new InputStreamReader(System.in));
25+
int t = nextInt();
26+
while (t-- > 0) {
27+
input();
28+
}
29+
br.close();
30+
}
31+
32+
public static void input() throws IOException {
33+
int n = nextInt();
34+
int[] candidate = new int[n];
35+
for (int i = 0; i < n; i++) {
36+
candidate[i] = nextInt();
37+
38+
}
39+
int k = nextInt();
40+
List<List<Integer>> ans = new ArrayList<>();
41+
List<Integer> ds = new ArrayList<>();
42+
combination(0, k, candidate, ans, ds);
43+
System.out.println(ans);
44+
45+
}
46+
47+
static void combination(int index, int target, int[] candidates, List<List<Integer>> ans,
48+
List<Integer> ds) {
49+
if (target == 0) {
50+
ans.add(new ArrayList<>(ds));
51+
return;
52+
}
53+
for (int i = index; i < candidates.length; i++) {
54+
if (i > index && candidates[i] == candidates[i - 1]) {
55+
continue;
56+
} else if (target < candidates[index])
57+
return;
58+
59+
ds.add(candidates[i]);
60+
combination(index + 1, target - candidates[i], candidates, ans, ds);
61+
ds.remove(ds.size() - 1);
62+
}
63+
}
64+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /