कुछ भद्दे कमेंट करने के बाद, यह समस्या पूरी शाम मेरे दिमाग में अटकी रही, और मैं अंततः निम्नलिखित सेट-आधारित दृष्टिकोण के साथ आया। मेरा मानना है कि यह निश्चित रूप से "सुरुचिपूर्ण" के रूप में योग्य है, लेकिन फिर मुझे यह भी लगता है कि यह "थोड़े गूंगा" के रूप में योग्य है। आप कॉल करें।
सबसे पहले, कुछ टेबल सेट करें:
-- For testing purposes
DROP TABLE Source
DROP TABLE Numbers
DROP TABLE Results
-- Add as many rows as need be processed--though note that you get N! (number of rows, factorial) results,
-- and that gets big fast. The Identity column must start at 1, or the algorithm will have to be adjusted.
-- Element could be more than char(1), though the algorithm would have to be adjusted again, and each element
-- must be the same length.
CREATE TABLE Source
(
SourceId int not null identity(1,1)
,Element char(1) not null
)
INSERT Source (Element) values ('A')
INSERT Source (Element) values ('B')
INSERT Source (Element) values ('C')
INSERT Source (Element) values ('D')
--INSERT Source (Element) values ('E')
--INSERT Source (Element) values ('F')
-- This is a standard Tally table (or "table of numbers")
-- It only needs to be as long as there are elements in table Source
CREATE TABLE Numbers (Number int not null)
INSERT Numbers (Number) values (1)
INSERT Numbers (Number) values (2)
INSERT Numbers (Number) values (3)
INSERT Numbers (Number) values (4)
INSERT Numbers (Number) values (5)
INSERT Numbers (Number) values (6)
INSERT Numbers (Number) values (7)
INSERT Numbers (Number) values (8)
INSERT Numbers (Number) values (9)
INSERT Numbers (Number) values (10)
-- Results are iteratively built here. This could be a temp table. An index on "Length" might make runs
-- faster for large sets. Combo must be at least as long as there are characters to be permuted.
CREATE TABLE Results
(
Combo varchar(10) not null
,Length int not null
)
ये रही दिनचर्या:
SET NOCOUNT on
DECLARE
@Loop int
,@MaxLoop int
-- How many elements there are to process
SELECT @MaxLoop = max(SourceId)
from Source
-- Initialize first value
TRUNCATE TABLE Results
INSERT Results (Combo, Length)
select Element, 1
from Source
where SourceId = 1
SET @Loop = 2
-- Iterate to add each element after the first
WHILE @Loop <= @MaxLoop
BEGIN
-- See comments below. Note that the "distinct" remove duplicates, if a given value
-- is to be included more than once
INSERT Results (Combo, Length)
select distinct
left(re.Combo, @Loop - nm.Number)
+ so.Element
+ right(re.Combo, nm.Number - 1)
,@Loop
from Results re
inner join Numbers nm
on nm.Number <= @Loop
inner join Source so
on so.SourceId = @Loop
where re.Length = @Loop - 1
-- For performance, add this in if sets will be large
--DELETE Results
-- where Length <> @Loop
SET @Loop = @Loop + 1
END
-- Show results
SELECT *
from Results
where Length = @MaxLoop
order by Combo
सामान्य विचार यह है:किसी भी स्ट्रिंग (जैसे, "ए") में एक नया तत्व ("बी" कहें) जोड़ते समय, सभी क्रमपरिवर्तनों को पकड़ने के लिए आप सभी संभावित पदों (बीए, एबी) में बी जोड़ देंगे, जिसके परिणामस्वरूप एक नया सेट होगा तार। फिर पुनरावृति:एक स्ट्रिंग में प्रत्येक स्थिति में एक नया तत्व (सी) जोड़ें (एबी कैब, एसीबी, एबीसी बन जाता है), सभी स्ट्रिंग्स (सीबीए, बीसीए, बीएसी) के लिए, और आपके पास क्रमपरिवर्तन का सेट है। अगले वर्ण के साथ सेट किए गए प्रत्येक परिणाम पर तब तक पुनरावृति करें जब तक कि आपके पास वर्ण... या संसाधन समाप्त न हो जाएं। 10 तत्व 3.6 मिलियन क्रमपरिवर्तन हैं, उपरोक्त एल्गोरिथम के साथ लगभग 48MB, और 14 (अद्वितीय) तत्व 87 बिलियन क्रमपरिवर्तन और 1.163 टेराबाइट तक पहुंचेंगे।
मुझे यकीन है कि इसे अंततः एक सीटीई में जोड़ा जा सकता है, लेकिन अंत में जो कुछ भी होगा वह एक गौरवशाली पाश होगा। तर्क इस तरह स्पष्ट है, और मैं मदद नहीं कर सकता लेकिन लगता है कि सीटीई निष्पादन योजना एक दुःस्वप्न होगी।