对策字符串google输出goog

来源:互联网 发布:淘宝广场舞健身球 编辑:程序博客网 时间:2024/05/22 06:19

题目:输入一个字符串,输出该字符串中对称的子字符串的最大长度。
比如输入字符串“google”,由于该字符串里最长的对称子字符串是“goog“

代码利用栈和队列实现:

// Stack1.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include<cstdio>#include<deque>#include<stack>using namespace std;int main(){//char *str="goyyogle";char *str="google";stack<char> cstack;//存储字符deque<char> cdeque;//存储对称字符串char tmpch;printf("检测字符串为%s\n",str);while(*str!='\0'){printf("%c",*str);if(cstack.empty()){cstack.push(*str);str++;continue;}tmpch=cstack.top();cstack.pop();if(tmpch==*str)//中间不存在单字符{cdeque.push_front(*str);cdeque.push_back(*str);str++;continue;}//if(!cstack.empty()&&cstack.top()==*str)//中间存在单字符//{//cdeque.push_front(tmpch);//cdeque.push_front(*str);//cdeque.push_back(*str);//cstack.pop(); //str++;//continue;//}cstack.push(tmpch);cstack.push(*str);str++;}printf("\n最大对称串...\n");while(0!=cdeque.size()){printf("%c",cdeque.front());cdeque.pop_front();}putchar('\n');system("pause");return EXIT_SUCCESS;}