练习使用字符串函数了。

1、字串的反转也是它的字串,2、最长,3、最先出现

string:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;

int main()
{
    int t, n; 
    string s;
    cin>>t;
    while(t--){
        cin>>s;
        int n = s.size();
        for(int i=n; i>=1; i--){//枚举长度
            for(int j=0; j+i-1<n; j++){//枚举开端
                string ts = s.substr(j, i);
                reverse(ts.begin(), ts.end());
                if(s.find(ts) != string::npos){
                    cout<<s.substr(j, i)<<endl;
                    goto here; 
                }
            }
        }
        here: ;
    }
    
    return 0;
}

char[],用时是上面的一半

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
using namespace std;
#define N  52

char s[N];
char ts[N];
int main()
{
    int t, n; 
    scanf("%d", &t);
    while(t--){
        scanf("%s", s);
        int n = strlen(s);
        for(int i=n; i>=1; i--){//枚举长度 
            for(int j=0; j+i-1<n; j++){//枚举开端 
                strncpy(ts, s+j, i);
                ts[i] = '\0'; 
                reverse(ts, ts+i);
                if(strstr(s, ts) != NULL){
                    strncpy(ts, s+j, i);
                    ts[i] = '\0'; 
                    cout<<ts<<endl;
                    goto here; 
                }
            }
        }
        here: ;
    }
    
    return 0;
}
 posted on 2015-04-04 16:51  平和之心  阅读(155)  评论(0编辑  收藏  举报