poj-1226

链接:https://vjudge.net/problem/POJ-1226

题意:
给你一些串,问你这些串中出现次数最多子串的长度,反向出现的也算

解题过程中希望实现的操作:

1)string按长度进行排序

2)字符串翻转(无意中翻到的er,发现还挺好用的)

#include<iostream>
#include <string>
#include <algorithm>
using namespace std;
int main(int argc, char*argv[])
{
string str = "song";
reverse(str.begin(), str.end());
//利用string中的函数直接进行翻转
return 0;
}

3)思考如何将字符串复制排序,以为有跟strcpy一样的函数呢
C++中的string是字符串类,它的对象就相当于基本类型int的变量一样,使用起来十分方便,用=号就能把一个对象的值赋给另一个对象

4)遍历可能的子串长度,对每个字符串进行查找,明天写代码

错误代码1

对0尾情况无法处理,还用了vector,自己都感动自己了

#include <iostream>
#include <string>
#include <cmath>
#include <algorithm>
#include <vector>

using namespace std;


int main()
{
    int t;
    cin >> t;
int i;
    while (t--)
    {
        std::vector<int> n;
        string x;
        cin>>x;
        i= x[0]=='-' ? 1 : 0;
        if(i==1)
        {

            cout<<"-";
        }
        for(; i<x.size(); i++)
           n.push_back(x[i] - '0');
        i = x[0]=='-' ? 1 : 0;
        std::reverse(n.begin()+i , n.end());
for(std::vector<int>::iterator it=n.begin(); it!=n.end(); ++it )
    cout << *it;
cout<<endl;

    }
}

错误代码2

1.前后翻转错误 2.负数处理不够好 3.0尾无法正确处理

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


int main()
{
    int t;
    cin >> t;
    string s;
    while (t--)
    {

        cin >> s;
        int i = 0;
        char t;
        if(s[0] == '-')
        {
            i++;
        }
        for(; i <= s.size() / 2+1; t = s[i], s[i] = s[s.size() - 1 - i], s[s.size() - 1 - i] = t, i++);
        for(int i=0; i<s.size(); i++)
            cout<<s[i];
        cout<<endl;
    }
}
原文地址:https://www.cnblogs.com/ygbrsf/p/12583023.html