2020大一寒假训练九(String)

1 取子字符串-string
Problem:1001
Time Limit:1000ms
Memory Limit:65536K
Description
编程实现在一个字符串中取出一部分字符的功能。取出的字符组成一个新字符串。

要求:编写自定义函数实现子字符串的选择,在main函数中将其输出。
提示:可以将母串、子串,开始,结束作为参数,实现求子串。
Input
输入的数据有多组,每组一行,前面是一个字符串,字符个数少于100个。后面是两个整数n和m,1<=n<=m<=100.n代表开始取字符位置,m代表结束位置。
Output
输出新字符串,输出母串中从n开始到m结束之间的所有字符。
Sample Input
goodafternoonabcedef 5 10
Thisisagoodbook 1 15
Sheisabeautifulgirl 2 10
Sample Output
aftern
Thisisagoodbook
heisabeau
Hint
Source

#include <bits/stdc++.h>

using namespace std;

int main()
{
    string s;
    while(cin >> s )
    {
        int n,m;
        cin >> n >> m ;
        int len =m-n+1;
        string x;
        x=s.substr(n-1,len);
        cout << x << endl;
    }
    return 0;
}

2 字符串处理-string
Problem:2132
Time Limit:1000ms
Memory Limit:65535K
Description
给出一个字符串S,计算S中有多少连续子串是回文串。
Input
输入包含多组测试数据。每组输入是一个非空字符串,长度不超过5000
Output
对于每组输入,输出回文子串的个数
Sample Input
aba
aa
Sample Output
4
3
Hint
输入样例二:
aaaaaa
输出样例二:
21
Source

#include <bits/stdc++.h>
using namespace std;

int main()
{
    string s,s1,tmp;
    while(cin>>s)
    {
        int num=s.size(),ans=0;
        for(int i=0;i<num;i++)
        {
            for(int j=1;j<=(num-i);j++)
            {
                s1=s.substr(i,j);
                tmp=s1;
                reverse(s1.begin(),s1.end());
                if(tmp==s1)
                    ans++;
            }

        }
        cout<<ans<<endl;
    }
    return 0;
}

3

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值