[英]How many different letters are in string
我必須編寫程序來計算字符串中有多少個不同的字母。 例如“abc”將給出3; 而且“abcabc”也會給3,因為只有3個不同的字母。
我需要使用pascal,但是如果你可以幫助使用不同語言的代碼,它也會非常好。
這是我的代碼不起作用:
var s:string;
i,j,x,count:integer;
c:char;
begin
clrscr;
Readln(s);
c:=s[1];
x:=1;
Repeat
For i:=1 to (length(s)) do
begin
If (c=s[i]) then
begin
delete(s,i,1);
writeln(s);
end;
end;
c:=s[1];
x:=x+1;
Until length(s)=1;
Writeln(x);
x是不同的字母計數器; 也許我的algorythm非常糟糕..任何想法? 謝謝。
你已經得到了如何做到的答案,這就是你的方式不起作用的原因。
首先,你有一個好主意:從字符串中的第一個字符開始,計算它(你忘了包含計數代碼),刪除字符串中所有出現的相同字符。 這個想法是低效的,但它會起作用。 你遇到了這段代碼的麻煩:
For i:=1 to (length(s)) do
begin
If (c=s[i]) then
begin
delete(s,i,1);
end;
end;
麻煩的是,Pascal在設置循環時將獲取Length(s)
值,但是你的代碼通過刪除chars來改變字符串的長度(使用delete(s,i,1)
)。 你最終會看到糟糕的記憶。 第二個問題是i
要前進,如果它匹配並刪除了一個字符並不重要。 這就是為什么這很糟糕。
Index: 12345
String: aabbb
你將測試i = 1,2,3,4,5,尋找a
。 當i
為1時你會找到一個匹配,刪除第一個字符,你的字符串將如下所示:
Index: 1234
String: abbb
您現在正在使用i = 2進行測試,並且它不匹配,因為s [2] = b。 你剛剛滑了a
,並且給定a
將在另一輪中保留在陣列中並使你的算法計算兩次。 “固定”算法如下所示:
i := 1;
while i <= Length(s) do
if (c=s[i]) then
Delete(s,i,1)
else
Inc(i);
這是不同的:在給定的示例中,如果我在1
處找到匹配,則光標不會前進,因此它會看到第二個a
。 另外因為我使用的是while
循環,而不是for
循環,所以我不能遇到for循環的可能實現細節。
您的算法還有另一個問題。 在循環中刪除字符串中所有出現的第一個字符串后,您將使用以下代碼准備下一個循環:
C:= S [1];
麻煩的是,如果你給這個算法提供一個形式為aa
(長度= 2,兩個相同的字符)的字符串,它將進入循環,刪除或出現a
(那些轉換成一個EMPTY字符串),然后嘗試讀取EMPTY字符串的第一個字符。
最后一句話:您的算法應該在輸入時處理空字符串,返回count = 0。 這是固定的算法:
var s:string;
i,count:integer;
c:char;
begin
Readln(s);
count:=0;
while Length(s) > 0 do
begin
Inc(Count);
c := s[1];
i := 1;
while i <= Length(s) do
begin
If (c=s[i]) then
delete(s,i,1)
else
Inc(i);
end;
end;
Writeln(Count);
Readln;
end.
我是Delphi的專家,所以我不太清楚Pascal是如何限制的。 不過,這是德爾福:
// Returns the number of *distinct* "ANSI" characters in Str
function NumChrs(const Str: AnsiString): integer;
var
counts: array[0..255] of boolean;
i: Integer;
begin
ZeroMemory(@counts[0], sizeof(boolean) * length(counts));
for i := 1 to length(Str) do
counts[ord(Str[i])] := true;
result := 0;
for i := 0 to high(counts) do
if counts[i] then
inc(result);
end;
第一行可以寫出來
for i := 0 to high(counts) do
counts[i] := false;
如果你不能使用Windows API(或Delphi FillChar
函數)。
如果您希望獲得Unicode支持(如Delphi 2009+),您可以這樣做
// Returns the number of *distinct* Unicode characters in Str
function NumChrs(const Str: string): integer;
const
AllocBy = 1024;
var
FoundCodepoints: array of integer;
i: Integer;
procedure Push(Codepoint: integer);
var
i: Integer;
begin
for i := 0 to result - 1 do
if FoundCodepoints[i] = Codepoint then
Exit;
if length(FoundCodepoints) = result then
SetLength(FoundCodepoints, length(FoundCodepoints) + AllocBy);
FoundCodepoints[result] := Codepoint;
inc(result);
end;
begin
result := 0;
for i := 1 to length(Str) do
Push(ord(Str[i]));
end;
這是我的版本。 如果你把它交給我,我並不是說你的作業會得到很好的印記。
function NumberOfUniqueChars(s: string): Integer;
var
i, j: Integer;
c: char;
begin
for i := 1 to Length(s) do
for j := i+1 to Length(s) do
if s[i]<s[j] then
begin
c := s[i];
s[i] := s[j];
s[j] := c;
end;
Result := 0;
for i := 1 to Length(s) do begin
if (i=1) or (s[i]<>c) then
inc(Result);
c := s[i];
end;
end;
並使用Delphi構造(效率不高,但干凈)
function returncount(basestring: String): Integer;
var charstrings: TStringList;
I:Integer;
begin
Result := 0;
charstrings := TStringlist.create;
try
charstrings.CaseSensitive := False;
charstrings.Duplicates := DupIgnore;
for I := 1 to length(basestring) do
charstrings.Add(basestring[i];
Result := charstrings.Count;
finally
charstrings.free;
end;
end;
不同的語言還可以?
紅寶石:
s = "abcabc"
=> "abcabc"
m = s.split(//)
=> ["a", "b", "c", "a", "b", "c"]
p = m & m
=> ["a", "b", "c"]
p.count
=> 3
Delphi版本。 與@The Duck鴨Python版相同的想法。
function GetNumChars(Str: string): Integer;
var
s: string;
c: Char;
begin
s := '';
for c in Str do
begin
if Pos(c, s) = 0 then
begin
s := s + c;
end;
end;
Result := Length(s);
end;
只需折騰set
替代......
program CountUniqueChars;
{$APPTYPE CONSOLE}
uses
SysUtils;
var
InputStr: String;
CountedChr: Set of Char;
TotalCount: Integer;
I: Integer;
begin
Write('Text: ');
ReadLn(InputStr);
CountedChr := [];
TotalCount := 0;
for I := 1 to Length(InputStr) do
begin
Write('Checking: ' + InputStr[i]);
if (InputStr[i] in CountedChr)
then WriteLn(' --')
else begin
Include(CountedChr, InputStr[i]);
Inc(TotalCount);
WriteLn(' +1')
end;
end;
WriteLn('Unique chars: ' + IntToStr(TotalCount));
ReadLn;
end.
在Python中,如果你想要它用於任何其他語言,請解釋:(因為你想要不同的語言)
s = 'aahdhdfrhr' #s is the string
l = [] #l is an empty list of some kind.
for i in s: #Iterate through the string
if i not in l: #If the list does not contain the character
l.append(i) #Add the character to the list
print len(l) #Print the number of characters in the list
function CountChars(const S:AnsiString):Integer;
var C:AnsiChar; CS:Set of AnsiChar;
begin
Result := 0;
CS := [];
for C in S do
if not (C in CS) then
begin
CS := CS + [C];
Inc(Result);
end;
end;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.