字符串习题2

771. 字符串中最长的连续出现的字符(双指针)

https://www.acwing.com/problem/content/773/

#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
int main()
{
    int n;
    cin >> n;
    while(n--)
    {
        string s;
        cin >> s;
        int ans = 0;
        char c;
        for(int i = 0; i < s.size(); i++){  //第一类双指针代码
            int j = i;
            while(j < s.size() && s[j] == s[i]) j++;
            if(j-i > ans) ans = j - i, c = s[i];
            i = j - 1;
        }                                   //
        cout << c << ' ' << ans << endl;
    }
    
    return 0;
}

 774. 最长单词

https://www.acwing.com/problem/content/776/

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

int main()
{
    string s,a;
    getline(cin,s);
    int l = s.size() - 1,ans=0,g;
    for(int i = 0; i < l; i++){  //双指针代码
        int j = i,m=0; string c;  
        while(s[j] != ' ' && j < l){ j++;}
        if(j-i > ans){ ans = j - i;
        for (int h = i; h < j; h ++ ){
            a[m] = s[h];m++;g=m;
        }
        }
        i = j;
    }
    for (int i = 0; i < g; i ++ ){
        cout << a[i];
    }
    
    return 0;
}

775. 倒排单词

https://www.acwing.com/problem/content/777/

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

int main()
{
    string s;
    getline(cin,s);
    for(int i = s.size()-1; i >= 0; i--){ //双指针代码
        int j = i;
        while(s[j] != ' ' && j >= 0){
            j--;
        }
        for (int k = j+1; k <= i; k ++ ) {cout << s[k];}
        cout << ' ';
        i = j;
    }
    
    return 0;
}

 776. 字符串移位包含问题

https://www.acwing.com/problem/content/778/

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

int main()
{
    string a,b;
    cin >> a >> b;
    if(a.size() < b.size()) swap(a,b);
    int b1 = b.size(),flag = 0,a1 = a.size();
    for (int i = 0; i < a1; i ++ ){
        int j = i,k=0;
        while(a[j] == b[k]){
            j++;k++;
            if(j >= a1){
                j = 0;
            }
        }
        if(k == b1){ flag = 1;cout << "true";}
    }
    if(flag == 0) cout << "false";
    
    return 0;
}

 777. 字符串乘方

https://www.acwing.com/problem/content/779/

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

int main()
{
    string s;
    while(cin >> s , s !="."){
        int l = s.size();
        for (int i = l; i ; i -- )
            if(l % i == 0)
            {
                int m = l / i;
                string s1 = s.substr(0,m);
                string r;
                for (int j = 0; j < i; j ++ ) r += s1;
                
                if(r == s){ 
                    cout << i << endl;
                    break;
                }
            }
        }
    return 0;
}

778. 字符串最大跨距

https://www.acwing.com/problem/content/780/

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

int main()
{
    string s,s1,s2;
    int l=0,r=0,len=0;
    getline(cin,s,',');
    getline(cin,s1,',');
    getline(cin,s2,'\n');
    
    l = s.find(s1);
    r = s.rfind(s2);
    
    if(l != -1 && r != -1 && l+s1.size()-1 < r){
        cout << r-l-s1.size() << endl;
    }
    else cout << -1 << endl;
    
    return 0;
}

 779. 最长公共字符串后缀

https://www.acwing.com/problem/content/781/

#include <iostream>
using namespace std;

int main()
{
    int n;
    while(cin>>n,n)
    {
        string a[n];      //用string数组来保存每行字符串
        for(int i=0;i<n;i++)
            cin>>a[i];
        int m=200,p=0;
        for(int i=1;i<n;i++)    //找出最短的字符串,记录长度m和位置p(可以并入上面读入操作同时进行)
        {
            if(a[i].size()<m)
            {
                m=a[i].size();
                p=i;
            }
        }
        string s=a[p]; int i;   //s即最短字符串,i+1记录最长公共后缀长度。
        for(i=0;i<m;i++)    //只需要枚举i在[0,m)的整数。
        {
            int j;
            for(j=0;j<n;j++)   //枚举string数组a[]的每一个元素(即每一行字符串),判断公共后缀是否匹配
                if(a[j][a[j].size()-1-i]!=s[m-1-i]) break;   //一旦不匹配,终止枚举

            if(j!=n) break;    //若j=n,表示每个字符串的(长度-1-i)位置是匹配的,再枚举i+1。否则,终止枚举

        }
        if(i==0) cout << endl;   //i=0表示没有公共后缀
        else cout << s.substr(m-i,i) << endl;  //i>0,表示有长度为i的最长公共后缀
    }

    return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

永夜天

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值