保小求大006

字符串19-字符串移位包含问题

题目描述:
对于一个字符串来说,定义一次循环移位操作为:将字符串的第一个字符移动到末尾形成新的字符串。

给定两个字符串s1和s2,要求判定其中一个字符串是否是另一字符串通过若干次循环移位后的新字符串的子串。例如CDAA是由AABCD两次移位后产生的新串BCDAA的子串,而ABCD与ACBD则不能通过多次移位来得到其中一个字符串是新串的子串。

输入:
一行,包含两个字符串,中间由单个空格隔开。字符串只包含字母和数字,长度不超过30。

输出:
如果一个字符串是另一字符串通过若干次循环移位产生的新串的子串,则输出true,否则输出false。

样例输入:

AABCD CDAA

样例输出:

true

代码如下:

//超时
#include<iostream>
#include<cstring>
using namespace std;
int main(){
    string a,b,s1,s2;
    int i,temp;
    cin>>s1>>s2;
    a = s1,b = s2;
    int len_a = a.length(),len_b = b.length();
    if(len_a < len_b){
        do{
            for(i = len_b - 1;i >= 0;i--){
                if(i == len_b - 1){
                    temp = b[i];
                    b[i] = b[i-1];
                }
                else if(i == 0){
                    b[i] = temp;
                }
                else
                    b[i] = b[i-1];
            }
        }while(b != s2 && b.find(a) == -1);
        if(b.find(a) != -1)
            cout<<"true"<<endl;
        else{
            cout<<"false"<<endl;
        }
    }
    else{
        do{
            for(i = len_a - 1;i >= 0;i--){
                if(i == len_a - 1){
                    temp = a[i];
                    a[i] = a[i-1];
                }
                else if(i == 0){
                    a[i] = temp;
                }
                else
                    a[i] = a[i-1];
            }
        }while(a != s1 && a.find(b) == -1);
        if(a.find(b) != -1)
            cout<<"true"<<endl;
        else{
            cout<<"false"<<endl;
        }
    }
    return 0;
}
// #include<iostream>
// #include<cstring>
// using namespace std;

// bool check(string s1,string s2){
//     s1 += s1;
//     if(s1.find(s2)!=-1 && s1.find(s2)<=s1.size()){
        
//         return true;
//     }
//     else
//         return false;
// }

// int main(){
//     string s1,s2;
//     cin>>s1>>s2;
//     if(check(s1,s2) || check(s2,s1)){
//         cout<<"true"<<endl;
//     }
//     else
//         cout<<"false"<<endl;
//     return 0;
// }

说明:
以上代码超时未AC,之后会修改


字符串20-删除单词后缀

题目描述:
给定一个单词,如果该单词以er、ly或者ing后缀结尾, 则删除该后缀(题目保证删除后缀后的单词长度不为0), 否则不进行任何操作。

输入:
输入一行,包含一个单词(单词中间没有空格,每个单词最大长度为32)。

输出:
输出按照题目要求处理后的单词。

样例输入:

referer

样例输出:

refer

代码如下:

#include<iostream>
#include<cstring>
using namespace std;
int main(){
    string s;
    cin>>s;
    int len = s.length();
    // cout<<len<<endl;
    if(s[len-1]=='g' && s[len-2]=='n' && s[len-3]=='i' && len>3){
        for(int i = 0;i < len-3;i++)
            cout<<s[i];
    }
    else if(s[len-1]=='y' && s[len-2]=='l' && len>2){
        for(int i = 0;i < len-2;i++)
            cout<<s[i];
    }
    else if(s[len-1]=='r' && s[len-2]=='e' && len>2){
        for(int i = 0;i < len-2;i++)
            cout<<s[i];
    }
    else
        cout<<s;

    return 0;
}

字符串21-单词替换

题目描述:
输入一个字符串,以回车结束(字符串长度<=100)。该字符串由若干个单词组成,单词之间用一个空格隔开,所有单词区分大小写。现需要将其中的某个单词替换成另一个单词,并输出替换之后的字符串。

输入:
输入包括3行,
第1行是包含多个单词的字符串 s;
第2行是待替换的单词a(长度 <= 100);
第3行是a将被替换的单词b(长度 <= 100).

s, a, b 最前面和最后面都没有空格.

输出:
输出只有 1 行,将s中所有单词a替换成b之后的字符串。

样例输入:

You want someone to help you
You
I

样例输出:

I want someone to help you

代码如下:

#include<iostream>
#include<string>
using namespace std;
string a[105];
int main(){
	//采用replace函数为解决
    string s;
    int n = 0;
    while(cin>>s){
        a[++n] = s;//n++;a[n]=s;
    }
    //读入的时候将想被替换的单词放在了n-1的位置
    //替换的单词放在了n的位置,所以循环到n-2即可
    for(int i = 1;i <= n-2;i++){
        if(a[i] == a[n-1])
            cout<<a[n]<<" ";
        else
            cout<<a[i]<<" ";
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值