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