當前位置:首頁 » 操作系統 » 迴文字元串演算法

迴文字元串演算法

發布時間: 2022-05-13 00:55:43

① 關於迴文演算法

這個函數不對奇偶長度的字元串都通用的 我給你稍微改了下 空都補上了
int IsHuiWen(char *s)
{
SeqStack T; // 定義一個順序棧T
char t;
Init Stack(T); //初始化棧
l=strlen(s); //變數l的值為字元串長度
for(i=0;i <= l/2;i++) Push(&T,s[i]); //i<=l/2和後面的push 作用就是把字元串前半截壓入棧結構
while(!EmptyStock(&T))
{
t=Pop(&T); //讓元素出棧並且賦給t 用來和後面的字元比較
if(t != s[l-i]) //如果中間的2個字元不相同就返回0
{return 0;}
--i; //如果第一次比較相同就比較第二次 從中間向兩邊比較直到棧為空
} // 這個後面的while是多餘的 沒用
return 1;//比較完後都不返回0就是迴文 返回1
}

② 使用棧判斷給定字元串是否是迴文的演算法

#include <stdio.h>
#define SIZE 50
int isPalindrome(char str[]);
int elementSize=0;
static int i=0;
int main()
{
int j=0,result;
char element,str[SIZE];
printf("請輸入字元串,以回車結束:\n");
/*以下用循環結構讀入字元數組的元素,防止了因字元串中含有空格而不能全部讀入的情況*/
scanf("%c",&element);
while(element!='\n')
{
str[j]=element;
elementSize++;//記錄了數組中已有元素的個數
j++;
scanf("%c",&element);
}

if(isPalindrome(str))
printf("該字元串是迴文字元串\n");
else
printf("該字元串不是迴文字元串\n");

// system("pause");
return 0;
}
/*函數功能:判斷字元串是否為迴文串*/
int isPalindrome(char str[])
{
/*把數組元素前後對應比較,即第一個元素與最後一個元素比較是否相等,依此類推*/
if(i>=elementSize-i-1)//說明是迴文串
return 1;

else if(str[i]==str[elementSize-i-1])
{
i++;//i為全局靜態變數
isPalindrome(str);
}

else //出現不相等的情況,說明不是迴文串,返回0
return 0;
}

③ 怎樣生成迴文字元串

#include <iostream>
#include <string>
using namespace std;

#define N 80

bool isHuiWen(string str)
{
int i;
for(i=0;i<str.length()-1-i;i++)
if(str[i]!=str[str.length()-1-i])
return 0;
return 1;
}

int getMin(string src,string &dest)
{
if(isHuiWen(src))
{
dest=src;
return 0;
}

char p[N];
int top=0;

string temp=src;
string tempL,tempR;
int m1,m2;

while(temp[0]==temp[temp.length()-1])
{
p[top++]=temp[0];
temp=temp.substr(1,temp.length()-2);
}

m1=getMin(temp[temp.length()-1]+temp,tempL);
m2=getMin(temp+temp[0],tempR);
if(m1<m2) dest=tempL;
else dest=tempR;

while(top>0)
{
top--;
dest=p[top]+dest;
dest+=p[top];
}

return dest.length()-src.length();
}

void main(void)
{
string str,result;
cin>>str;
getMin(str,result);
cout<<result<<endl;
}

④ Delphi程序設計,輸入任意字元串,判斷其是否為迴文字元串,要求用2種演算法(利用FOR循環或WHILE循環)

試編寫代碼如下:

//根據正向、反向相同位置字元對比
functionisHuiWen1(testStr:string):Boolean;
vari,iCount:Integer;
begin
Result:=True;

i:=1;
iCount:=Length(testStr);
while(i<iCount)do
begin
if(testStr[i]<>testStr[iCount])then
begin
Result:=False;
Break;
end;
Inc(i);
Dec(iCount);
end;
end;

//將字元串倒序與原字元對比
//可以直接使用倒序函數,需引入StrUtils
functionisHuiWen2(testStr:string):Boolean;
var
i,iCount:Integer;
str:string;
begin
str:='';
iCount:=Length(testStr);
fori:=iCountdownto1do
str:=str+testStr[i];

Result:=str=testStr;
end;

procereTForm1.btnOneClick(Sender:TObject);
var
i,count,len:Integer;
begin
count:=Length(edtStr.Text);
ifcount<3then
begin
ShowMessage('輸入字元數太少!');
Exit;
end;
ifisHuiWen1(edtStr.Text)then
ShowMessage('輸入的是迴文字元串。')
else
ShowMessage('不是迴文字元串。')
end;

procereTForm1.btnTwoClick(Sender:TObject);
var
i,count,len:Integer;
begin
count:=Length(edtStr.Text);
ifcount<3then
begin
ShowMessage('輸入字元數太少!');
Exit;
end;
ifisHuiWen2(edtStr.Text)then
ShowMessage('輸入的是迴文字元串。')
else
ShowMessage('不是迴文字元串。')
end;


運行截圖如下:


⑤ 測試字元串是否迴文,求不同的演算法

#include<stdio.h>
int main()
{
int i;
char a[100];
char *p, *q;
for (i = 0; (i<100) && ((a[i] = getchar()) != '\n'); i++);
p = &a[0];
q = &a[i - 1];
while (p<q)
{
if (*p == *q)
{
p++;
q--;
}
else
{
printf("not huiwen!");
break;
}
}
if (p >= q)printf("huiwen!");
getchar();
return 0;
}
用兩個指針分別指向這個字元串的頭和尾,然後判斷指針指向的值如果相等就向中間移動直到頭指針達到或超過尾指針(結論是迴文),在上面的過程中如果跳出循環就說明頭尾的值不相等(結論不是迴文)

c語言編程:判定一個字元是否是迴文串(迴文串是指從開頭讀和從末尾讀均為相同字元的字元串,例如:abcba

1、首先,在C語言軟體中,定義多個整型變數,保存程序中所需操作的數值。

⑦ C語言判斷迴文字元串

//你的錯誤在於遞歸調用的時候,i每次都是0,所以要用static int i = 0
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int hui(char a[10000])
{
int n;
static int i=0; //這里改為static int i = 0就對了,因為你遞歸調用i的時候每次都把i置為0了,那肯定不行的,因為你的i是在變得,所以用static
n=strlen(a);

if(i>=n-i-1)
return 1;
else
{
if (a[i]==a[n-i-1])
{
i++;
hui(a);
}
else
return 0;
}
}
int main(int argc, char *argv[])
{
char a[10000];
gets(a);
if (strlen(a)==0) {printf("No\n");}
else
{
if(hui(a))
printf("Yes\n");
else
printf("No\n");
}
return 0;
}

⑧ c語言求迴文子串的個數的高效演算法

class Palindrome {
public:
int getLongestPalindrome(string A, int n) {
int max=0,count=0;
for(int i=0;i<n;i++) //i作為迴文串的中心
{
for(int j=0;((i-j)>=0)&&((i+j)<n);j++)//若迴文串是奇數個,i中心前面有j個,後面有j個
{
if(A[i-j]!=A[i+j])
break;
count=j*2+1;
}
if(max<count)
max=count;
for(int j=0;((i-j)>=0)&&((i+1+j)<n);j++)//若迴文串是偶數個,i和i+1是中心,前面有j個,後面有j個
{
if(A[i-j]!=A[i+1+j])
break;
count=j*2+2;
}
if(max<count)
max=count;
}
return max;

}

};

⑨ 迴文字元串——遞歸

#include<stdio.h>
#include<string.h>

int main()
{
int i=0,n,k=0;
char a[20],*p,*q;
scanf("%s",a);
n=strlen(a);
p=a; q=p+n-1;
while(i<(n/2+1)&&q>p)
if(*p==*q) { k++;i++; p++; q--; }
if(k==n/2) printf("Yes\n");
else printf("No\n");

system("pause");
return 0;
}
這是對你的簡化版。錯誤之處一一道來:
1,while語句q>p有錯誤,因為其後的p,q一直未變。
2,while中i<n多餘,只需到n/2+1即可。
3,if語句應該是字元比較 *(p+i)==*(q-i)
4,if語句 { k++; i++; } 必須合在一起,不然i是不變的。
5,if語句,p,q沒有變。所以while中只是i<n有效
6,最後判斷n%2取余沒有比較。

⑩ 迴文字元串——遞歸。C語言。

#include<stdio.h>
#include<string.h>
char _str[1005];
int IsH(char _str[],int _L,int _R)
{
if(_L>=_R)return 1;
if(_str[_L]-_str[_R])
return 0;
return IsH(_str,_L+1,_R-1);
}
void main()
{
while(gets(_str),strcmp(_str,"#"))//輸入#結束
printf("%s\n",IsH(_str,0,strlen(_str)-1)?"Yes":"No");
}

熱點內容
伺服器cpu能供多少電腦使用 發布:2024-10-09 23:05:21 瀏覽:349
演算法和嵌入式 發布:2024-10-09 23:04:34 瀏覽:553
谷歌內部伺服器錯誤是什麼意思 發布:2024-10-09 22:39:27 瀏覽:904
java中todate 發布:2024-10-09 22:01:49 瀏覽:854
android簡訊許可權設置 發布:2024-10-09 21:45:43 瀏覽:849
安卓手機轉移數據為什麼自動斷開 發布:2024-10-09 21:40:52 瀏覽:88
什麼是海關防盜密碼鎖 發布:2024-10-09 21:32:24 瀏覽:852
shell腳本的加減 發布:2024-10-09 21:23:23 瀏覽:402
安卓平板和蘋果的平板有什麼區別 發布:2024-10-09 20:26:37 瀏覽:428
上傳速度對網速的影響嗎 發布:2024-10-09 20:09:38 瀏覽:564