(*---
Doc du lieu:
n: chieu dai xau s,
k: vi tri xau thu cap trong day da sap ---*) ---*) procedure Doc; var i: integer; begin assign(f,fn); reset(f); readln(f,n,k);
for i:=1 to n do read(f,s[i]); close(f);
end;
(*--- So sanh 2 xau thu cap [i] va [j]. So sanh 2 xau thu cap [i] va [j]. Sanh(i,j)
= 0: neu [i] = [j] = -1: neu [i] < [j] = 1 neu [i] > [j]
---*) function Sanh(i,j: integer): integer; function Sanh(i,j: integer): integer; var k: integer; begin for k:=1 to n do begin if s[i] <> s[j] then begin
if s[i] < s[j] then Sanh:=-1 else Sanh:=1;
exit; end;
if i=n then i:=1 else inc(i); if j=n then j:=1 else inc(j); end;
Sanh:=0; end;
(*---
Tim phan tu thu k
---*) function Find(k: integer):integer; function Find(k: integer):integer;
var d, c, i, j, m, y: integer; begin d:=1 ; c:=n; while d <= c do begin i:=d; j:=c;
m:=id[(i+j) div 2]; {phan tu giua} while i <= j do
begin
while Sanh(m,id[j])<0 do dec(j); {doan phai}
{doi cho neu can}
if (i <= j) then begin y:= id[i]; id[i]:= id[j]; id[j]:= y; inc(i); dec(j); end; end; if j < k then d:=i; if k < i then c:=j; end; find:=id[k]; end; {--- Ghi ket qua vao tep
---} procedure Ghi(k: integer); procedure Ghi(k: integer);
var i: integer; begin assign(g,gn); rewrite(g); for i:=1 to n do begin write(g,s[k]);
if k=n then k:=1 else inc(k); end; close(g); end; procedure run; var i:integer; begin Doc;
for i:=1 to n do id[i]:=i; Ghi(find(k)); end; BEGIN run; END. // C# using System; using System.IO; namespace SangTao1 { /*--- * Tim xau mau thu k voi do phuc tap 2N * ---*/ class abc
{
const int mn = 500;
const string gn = "abc.out"; static string s;
static int n = 0; // chieu dai xau mau static int k = 0; // xau thu k
static int[] id; static void Main() {
Run();
Console.ReadLine(); } // Main
static void Run() {
Doc();
Console.WriteLine(n + " " + k + " " + s); id = new int[n];
for (int i = 0; i < n; ++i) id[i] = i; PhanTuGiua(); Ghi(); Test(); Console.WriteLine("\n Fini"); } /*--- * Ghi dong thu k trong
* day da sap vao file gn
* ---*/ static void Ghi() static void Ghi()
{
StreamWriter g = File.CreateText(gn); int j = id[k-1];
for (int i = 0; i < n; ++i) g.Write(s[(j + i) % n]); g.Close();
}
/*--- * Hien thi dong thu s[j...] * Hien thi dong thu s[j...] * ---*/ static void PrintLine(int j)
{
for (int i = 0; i < n; ++i)
Console.Write(s[(j + i) % n]); Console.WriteLine();
}
static void Doc() {
char[] cc = new char[] { ' ', '\n', '\t', '\r'};
string [] ss = (File.ReadAllText(fn)).Split(cc, StringSplitOptions.RemoveEmptyEntries); n = int.Parse(ss[0].Trim()); // do dai xau mau k = int.Parse(ss[1].Trim()); // xau thu k s = ss[2]; // xau mau
static void PhanTuGiua() // Tim phan tu thu k { int m; int d = 0; int c = n - 1; int i = 0; int j = 0; int t = 0;
int k0 = k - 1; // xau thu k tinh tu 1 while (d <= c)
{
i = d; j = c; j = c;
m = id[(i + j) / 2];
while (Sanh(id[i], m) < 0) ++i; while (Sanh(m, id[j]) < 0) --j; if (i <= j)
{
t = id[i]; id[i] = id[j]; id[j] = t; ++i; --j; ++i; --j; } if (j < k0) d = i; if (k < i) c = j; } }
// so sanh 2 xau thu cap s[x...] va s[y..] static int Sanh(int x, int y)
{
int ix = 0; int iy = 0;
for (int i = 0; i < n; ++i) {
ix = (x + i) % n; iy = (y + i) % n; if (s[ix] != s[iy])
return (s[ix] < s[iy]) ? -1 : 1; }
return 0; }
static void IdQSort(int d, int c)// sap theo chi dan { int i = d; int j = c; int m = id[(i + j) / 2]; int t = 0; while (i <= j) {
while (Sanh(id[i], m) < 0) ++i; while (Sanh(m, id[j]) < 0) --j; if (i <= j)
{
t = id[i]; id[i] = id[j]; id[j] = t; ++i; --j; ++i; --j;
} } } if (d < j) IdQSort(d, i); if (i < c) IdQSort(i, c); } /*--- * Kiem tra lai bang cach dung * thuat toan QSort theo chi dan * ---*/ static void Test()
{
Console.WriteLine("\n Xau mau: " + s); for (int i = 0; i < n; ++i) id[i] = i; IdQSort(0, n - 1);
Console.WriteLine("\n Day sap tang: \n"); for (int i = 0; i < n; ++i)
{
Console.Write((i + 1) + ". "); PrintLine(id[i]);
}
Console.WriteLine();
Console.WriteLine("\n Xau thu " + k); PrintLine(id[k-1]);
Console.WriteLine("\n Xau ghi trong file " + gn);
Console.WriteLine(File.ReadAllText(gn)); }
} // class } // SangTao1