forked from t3nsor/SPOJ
-
Notifications
You must be signed in to change notification settings - Fork 2
/
bwheeler.cpp
72 lines (72 loc) · 1.2 KB
/
bwheeler.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// 2008-08-20
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstring>
using namespace std;
typedef pair<int,int> pii;
char grid[1000][1000];
pii arr[1000];
int N;
pii radixarr[26][1000];
void order(int sc)
{
int i=0;
int j,k,m,cnt;
int lettercnt[26];
char c;
while (i<N)
{
j=i+1;
while (j<N&&arr[i].first==arr[j].first)
j++;
if (j-i>1)
{
memset(lettercnt,0,sizeof(lettercnt));
for (k=i; k<j; k++)
{
c=grid[sc][arr[k].second]-'a';
radixarr[c][lettercnt[c]++]=arr[k];
}
cnt=i;
for (k=0; k<26; k++)
for (m=0; m<lettercnt[k]; m++)
arr[cnt++]=radixarr[k][m];
for (k=i+1; k<j; k++)
if (grid[sc][arr[k].second]==grid[sc][arr[k-1].second])
arr[k].first=arr[k-1].first;
else
arr[k].first=k;
}
i=j;
}
}
int main()
{
int spos,i,j;
char s[1001];
for(;;)
{
scanf("%d",&spos);
if (!spos) return 0;
spos--;
getchar();
gets(s);
N=strlen(s);
strcpy(grid[N-1],s);
for (i=0; i<N; i++)
{
arr[i].first=0;
arr[i].second=i;
}
order(N-1);
for (i=0; i<N-1; i++)
{
for (j=0; j<N; j++)
grid[i][j]=grid[(i+N-1)%N][arr[j].second];
order(i);
}
for (i=0; i<N; i++)
putchar(grid[i][spos]);
putchar('\n');
}
}