HackerEarth Micro and String
/**
* @Author: Pranta Sarker
*
*/
#include<bits/stdc++.h>
using namespace std;
#define fast ios_base::sync_with_stdio(0)
#define bfast cin.tie(0)
#define outs(x) cout << x << " "
#define outn(x) cout << x << "\n"
#define sf scanf
#define pf printf
#define pfn(x , k) printf(k , x)
#define nl puts("")
#define psb push_back
#define mset(c,v) memset(c , v , sizeof c)
#define loop0(n) for(int i=0; i<n; i++)
#define loop1(n) for(int i=1; i<=n; i++)
#define mpair(x , y) make_pair(x , y)
#define all(x) x.begin(), x.end()
#define pi acos(-1.0)
#define psb push_back
#define clr clear()
typedef unsigned long long ull;
typedef long long LL;
typedef vector<int>vii;
typedef vector<LL>vll;
typedef vector<string>vs;
typedef map<int, int>mpii;
typedef map<string, int>mpsi;
typedef map<char, int>mpci;
typedef map<LL, LL>mpll;
const int mod = 1000007;
const int high = 1e4+5;
struct data
{
int index , tup[2];
data() { }
data(int x)
{
tup[0] = tup[1] = index = x;
}
bool operator < (const data &a)const
{
return tup[0] != a.tup[0] ? tup[0] < a.tup[0] : tup[1] < a.tup[1];
}
}arr[high];
int Rank[30][high];
void cln(int len)
{
for(int i=0; i<len; i++)
{
arr[i].tup[0] = arr[i].tup[1] = 0;
}
}
void buildSuffixArray(string s)
{
int i , j , len=s.length(), step=1, jump=1;
cln(len);
for(i=0; i<len; i++)
{
Rank[0][i] = s[i];
}
for(step=1, jump=1; jump<=len; step+=1, jump*=2)
{
for(i=0; i<=len; i++)
{
arr[i].index = i;
arr[i].tup[0] = Rank[step-1][i];
arr[i].tup[1] = i+jump < len ? Rank[step-1][i+jump] : -1;
}
sort(arr, arr+len);
Rank[step][arr[0].index] = 0;
for(i=1; i<len; i++)
{
int id1 = arr[i].index;
int id2 = arr[i-1].index;
Rank[step][id1] = arr[i-1].tup[0] == arr[i].tup[0] && arr[i-1].tup[1] == arr[i].tup[1] ? Rank[step][id2] : i;
}
}
}
int main()
{
fast;
string str;
while(cin >> str)
{
//outn(str);
str += "$";
buildSuffixArray(str);
// for(int i=0; i<str.length(); i++)
// {
// cout << arr[i].index << " " << str.substr(arr[i].index) << "\n";
// }
for(int i=1; i<str.length(); i++)
{
cout << arr[i].index << " ";
}
cout << "\n";
}
return 0;
}
* @Author: Pranta Sarker
*
*/
#include<bits/stdc++.h>
using namespace std;
#define fast ios_base::sync_with_stdio(0)
#define bfast cin.tie(0)
#define outs(x) cout << x << " "
#define outn(x) cout << x << "\n"
#define sf scanf
#define pf printf
#define pfn(x , k) printf(k , x)
#define nl puts("")
#define psb push_back
#define mset(c,v) memset(c , v , sizeof c)
#define loop0(n) for(int i=0; i<n; i++)
#define loop1(n) for(int i=1; i<=n; i++)
#define mpair(x , y) make_pair(x , y)
#define all(x) x.begin(), x.end()
#define pi acos(-1.0)
#define psb push_back
#define clr clear()
typedef unsigned long long ull;
typedef long long LL;
typedef vector<int>vii;
typedef vector<LL>vll;
typedef vector<string>vs;
typedef map<int, int>mpii;
typedef map<string, int>mpsi;
typedef map<char, int>mpci;
typedef map<LL, LL>mpll;
const int mod = 1000007;
const int high = 1e4+5;
struct data
{
int index , tup[2];
data() { }
data(int x)
{
tup[0] = tup[1] = index = x;
}
bool operator < (const data &a)const
{
return tup[0] != a.tup[0] ? tup[0] < a.tup[0] : tup[1] < a.tup[1];
}
}arr[high];
int Rank[30][high];
void cln(int len)
{
for(int i=0; i<len; i++)
{
arr[i].tup[0] = arr[i].tup[1] = 0;
}
}
void buildSuffixArray(string s)
{
int i , j , len=s.length(), step=1, jump=1;
cln(len);
for(i=0; i<len; i++)
{
Rank[0][i] = s[i];
}
for(step=1, jump=1; jump<=len; step+=1, jump*=2)
{
for(i=0; i<=len; i++)
{
arr[i].index = i;
arr[i].tup[0] = Rank[step-1][i];
arr[i].tup[1] = i+jump < len ? Rank[step-1][i+jump] : -1;
}
sort(arr, arr+len);
Rank[step][arr[0].index] = 0;
for(i=1; i<len; i++)
{
int id1 = arr[i].index;
int id2 = arr[i-1].index;
Rank[step][id1] = arr[i-1].tup[0] == arr[i].tup[0] && arr[i-1].tup[1] == arr[i].tup[1] ? Rank[step][id2] : i;
}
}
}
int main()
{
fast;
string str;
while(cin >> str)
{
//outn(str);
str += "$";
buildSuffixArray(str);
// for(int i=0; i<str.length(); i++)
// {
// cout << arr[i].index << " " << str.substr(arr[i].index) << "\n";
// }
for(int i=1; i<str.length(); i++)
{
cout << arr[i].index << " ";
}
cout << "\n";
}
return 0;
}
Comments
Post a Comment