Skip to content

Commit 3f8811d

Browse files
committed
Task 3: Added CLI arguments and copied the c++ file
1 parent 060fdd0 commit 3f8811d

File tree

24 files changed

+155
-4
lines changed

24 files changed

+155
-4
lines changed

Task_3/180070035_2.cpp

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
#include <iostream>
2+
#include <string>
3+
#include <algorithm>
4+
using namespace std;
5+
6+
long long int factorial(int n)
7+
{
8+
if (n==0 || n==1) return 1;
9+
else return n*factorial(n-1);
10+
}
11+
12+
long long int num_anagrams(int freq[26])
13+
{
14+
int n=0;
15+
16+
for (int i=0; i<26; i++)
17+
{
18+
n = n+freq[i];
19+
}
20+
long long int numerator = factorial(n);
21+
long long int denominator = 1;
22+
for (int i=0; i<26; i++)
23+
{
24+
denominator = denominator*factorial(freq[i]);
25+
}
26+
return numerator/denominator;
27+
}
28+
29+
long long int rank1(string s)
30+
{
31+
if (s.size() == 0 || s.size() == 1) return 0;
32+
long long int base_term = 0;
33+
int freq[26];
34+
for (int i=0; i<26; i++)
35+
{
36+
freq[i] = 0;
37+
}
38+
for (int i=0 ; i<s.size(); i++)
39+
{
40+
freq[s[i] - 'a']++;
41+
} //here a frequency chart is ready
42+
for (int i=0; i< s[0] - 'a'; i++ )
43+
{
44+
int freqc[26];
45+
for (int i=0; i<26; i++) freqc[i] = freq[i];
46+
if (freqc[i] == 0) continue;
47+
freqc[i]--;
48+
base_term = base_term + num_anagrams(freqc);
49+
}
50+
int n=s.size();
51+
string s1 = s.substr(1);
52+
long long int r = rank1(s1);
53+
return base_term + r;
54+
//return 0;
55+
}
56+
57+
string proxy(int freq[26])
58+
{
59+
string s = "";
60+
for (int i=0; i<26; i++)
61+
{
62+
char add = 'a'+i;
63+
for (int j=0; j<freq[i]; j++)
64+
{
65+
s = s+add;
66+
}
67+
}
68+
return s;
69+
}
70+
71+
string ans(string s, long long int n)
72+
{
73+
int a = s.size();
74+
if (a == 0 || a == 1) return s;
75+
int freq[26];
76+
for (int i=0; i<26; i++)
77+
{
78+
freq[i] = 0;
79+
}
80+
int m = s.size();
81+
for (int i=0; i<m; i++)
82+
{
83+
freq[s[i] - 'a']++;
84+
} //freq chart ready
85+
86+
long long int cumu_sum = 0;
87+
long long int prev_sum = 0;
88+
int i;
89+
int freqc[26];
90+
for (i=0; i<26; i++)
91+
{
92+
93+
if (freq[i] == 0) continue;
94+
//int freqc[26];
95+
for (int j=0; j<26; j++) {freqc[j] = freq[j];}
96+
freqc[i]--;
97+
prev_sum = cumu_sum;
98+
cumu_sum = cumu_sum + num_anagrams(freqc);
99+
if (cumu_sum > n) break;
100+
//this is the i we want to start with
101+
}
102+
103+
char first = 'a'+i;
104+
string pr = proxy(freqc);
105+
//cout<<"hi"<<endl;
106+
string pred = ans(pr, n-prev_sum);
107+
return first+pred;
108+
}
109+
110+
int main()
111+
{
112+
string s;
113+
long long int n;
114+
115+
cin>>s>>n;
116+
long long int r1 = rank1(s);
117+
string t = ans(s, n);
118+
//string t = "hello";
119+
cout<<r1<<" "<<t<<endl;
120+
}

Task_3/temp/inputs/0.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pcaqt 41

Task_3/temp/inputs/1.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
e 0

Task_3/temp/inputs/2.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
vzkbmjpihd 2596338

Task_3/temp/inputs/3.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
yisgasesfj 518932

Task_3/temp/inputs/4.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
jsdpzccxnk 1275732

Task_3/temp/inputs/5.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
icjdbzfjttcgbqbmvq 887480420

Task_3/temp/inputs/6.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
zqwlbevbitapfwomdq 1967958833

Task_3/temp/inputs/7.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
voxwangkqptxgqt 1273439332

Task_3/temp/inputs/8.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
eurcchogffmdaiepdc 1050498024

0 commit comments

Comments
 (0)