有关字符串的笔试题

小总结
1.

//转换字符串格式为原来字符串里的字符+该字符连续出现的个数
//1233422222转换为1121324125
//思路:两个vector来存放,一个存放字符,一个存放次数
void printChTe(string str)
{
    vector<int> myTime;
    vector<char> myChar;
    int i = 0; 
    while(i < str.length())
    {
        int time = 1;
        char tmp = str[i];
        i++;
        while (str[i] == str[i - 1])
        {
            time++;
            i++;
        }
        myTime.push_back(time);
        myChar.push_back(tmp);
    }
    auto it = myChar.begin();
    auto it2 = myTime.begin();
    for (it; it != myChar.end(); ++it)
    {
        cout << *it;
        cout << *it2;
        it2++;
    }
    cout << endl;
}

int main()
{
    string str;
    cin >> str;
    printChTe(str);
    return 0;
}

2.

//C++中strstr函数的实现,只要再加一个封装就好了,大部分功能就是如此
int Strstr(string str1, string str2)//判断str2是不是str1的子串
{
    int len1 = str1.length();
    int len2 = str2.length();
    for (int i = 0; i < len1; ++i)
    {
        int j = 0;
        int z = i;
        while (str2[j] == str1[z] && j < len2)
        {
            j++;
            z++;
        }
        if (j == len2)
        {
            return i;
        }
    }
    return -1;
}


int main()
{
    string str1;
    string str2;
    getline(cin, str1);
    getline(cin, str2);
    int local = Strstr(str1, str2);
    for (int i = local; i < str1.length(); ++i)
    {
        cout << str1[i];
    }
    cout << endl;

    return 0;
}

3.

//输入一行字符串,找出其中出现的相同且长度最长的字符串,输出它及首字符出现的位置
///abcdeabd
//bcdeabd
//cdeabd
void printChild(string str)
{
    vector<string> cha;
    int len = str.length();
    for (int i = 0; i < len; ++i)
    {
        string tmp;
        for (int j = i; j < len; ++j)
        {
            tmp += str[j];
        }
        cha.push_back(tmp);
    }
    int local = 0;
    int max = 0;
    int z = 0;
    auto it1 = cha.begin();
    for (it1; it1 != cha.end() - 1; ++it1)
    {
        string str1 = *it1;
        z++;
        for (auto it2 = it1 + 1; it2 != cha.end(); it2++)
        {
            string str2 = *it2;
            int i = 0;
            while (str1[i] == str2[i])
            {
                i++;//多少个相等
            }
            if (i > max)
            {
                max = i;
                local = z;
            }
        }
    }
    for (int i = 0; i < max; ++i)
    {
        cout << str[i + local - 1];
    }
    cout << endl;
    cout << local << endl;



}

int main()
{
    string str;
    getline(cin, str);
    printChild(str);

    return 0;
}

4.

//连续子串最多
//先把所有子串找出来
//依次存取所有子串连续的最大次数,然后比较输出
int getNum(string str1, string str2)//str2在str1中最多有连续多少个
{
    int i = 0;
    int len1 = str1.length();
    int len2 = str2.length();
    int time = 0;
    while (i < len1)
    {
        int j = 0;
        int len = 0;
        int tim = 0;
        while (str1[i] == str2[j])
        {
            i++;
            j++;
            if (j == len2)
            {
                j = 0;
                tim++;
            }
        }

        i++;
        if (time < tim)
        {
            time = tim;
        }
    }
    return time;
}

void printMaxChild(string str, int len)
{
    vector<string> cha;
    vector<int> size;
    for (int i = 1; i <= len; ++i)
    {
        for (int j = 0; j <= len - i; ++j)
        {
            int lon = i;//存入的子串长度
            string tmp;
            for (int z = 0; z < i; ++z)
            {
                tmp += str[j + z];
            }
            cha.push_back(tmp);
        }
    }

    auto ite = cha.begin();
    for (ite; ite != cha.end(); ++ite)
    {
        int num = getNum(str, *ite);
        size.push_back(num);
        //cout << num << endl;
    }
    auto it = size.begin();
    int max = *it;
    int time = 0;//第一个
    int flag = 0;
    it++;
    for (it; it != size.end(); ++it)
    {
        time++;
        if (*it > max)
        {
            flag = time;
            max = *it;
        }
    }
    string s;
    auto ite2 = cha.begin();
    int i = 0;
    while (i < flag)
    {
        i++;
        ite2++;
    }
    s = *ite2;

    cout << "出现最多次数" << max << endl;
    cout << "字符串是" << s << endl;
}



int main()
{
    string str;
    getline(cin, str);
    cout << str << endl;
    //  cout << str.length() << endl;//不算'\0'
    printMaxChild(str, str.length());



    return 0;
}

5.

//求删除多少个字符能够得到最大的回文
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
const int MAX = 1001;
int MaxLen[MAX][MAX]; //最长公共子序列,动态规划求法
int maxLen(string s1, string s2) {
    int length1 = s1.size();
    int length2 = s2.size();
    for (int i = 0; i < length1; ++i)
        MaxLen[i][0] = 0;
    for (int i = 0; i < length2; ++i)
        MaxLen[0][i] = 0;

    for (int i = 1; i <= length1; ++i)
    {
        for (int j = 1; j <= length2; ++j)
        {
            if (s1[i - 1] == s2[j - 1]) {
                MaxLen[i][j] = MaxLen[i - 1][j - 1] + 1;
            }
            else
            {
                MaxLen[i][j] = max(MaxLen[i - 1][j], MaxLen[i][j - 1]);
            }
        }
    }
    return MaxLen[length1][length2];
}

int main() {
    string s;
    while (cin >> s) {
        int length = s.size();
        if (length == 1) {
            cout << 1 << endl;
            continue;
        }
        //利用回文串的特点
        string s2 = s;
        reverse(s2.begin(), s2.end());
        int max_length = maxLen(s, s2);
        cout << length - max_length << endl;
    }
    return 0;
}

6.
快速排序

void fastPai(int *a, int start, int end)
{
    //1 9 8 7 6 5 4 3 2 1
    if (start >= end)
    {
        return;
    }
    int first = start;
    int last = end;
    int tmp = a[first];
    while (first < last)
    {
        while (a[last] >= tmp&&first < last)
        {
            last--;
        }

        a[first] = a[last];

        while (a[first] <= tmp&&first < last)
        {
            first++;
        }

        a[last] = a[first];

    }
    a[first] = tmp;
    fastPai(a, start, first - 1);
    fastPai(a, first + 1, end);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值