您的位置:首页 > 其它

字符串左旋转&&反转单词顺序&&Text Reverse&&句子的逆序

2016-08-08 11:08 399 查看

题目1:

字符串的左旋转操作是把字符串前面的若干字符转移到字符串的后面。请定义一个函数实现字符串左旋转操作的功能,

比如:输入字符串"abcdefg"和数字2,该函数将返回左旋转2位得到的结果"cdefgab";

思路:

这道题和翻转单词顺序很相似。思路也是一样的。

第一步:翻转整个字符串"abcdefg",得到"gfedcba"

第二步:翻转字符串“gfedc”,得到"cdefg"

第三步:翻转字符串"ba",得到"ab"

或者:

第一步:翻转字符串“ab”,得到"ba";

第二步:翻转字符串"cdefg",得到"gfedc";

第三步:翻转字符串"bagfedc",得到"cdefgab";

针对先全反转的情况代码

void LeftReverse(char* s,int num)
{
if (s == NULL || num<0 || num>strlen(s))
{
cout << "invalid input";
return;
}
char* begin = s;
char* end = s+strlen(s)-1;
Reverse(begin,end);//全反转
char* mid = s+strlen(s)-num-1;
Reverse(begin,mid);//反转前半部分
Reverse(mid+1,end);//反转后半部分
}
void Reverse(char *pBegin, char *pEnd)
{
<span style="white-space:pre">	</span>if (pBegin == NULL || pEnd == NULL)
<span style="white-space:pre">		</span>return;

<span style="white-space:pre">	</span>while (pBegin < pEnd)
<span style="white-space:pre">	</span>{
<span style="white-space:pre">		</span>char temp = *pBegin;
<span style="white-space:pre">		</span>*pBegin = *pEnd;
<span style="white-space:pre">		</span>*pEnd = temp;

<span style="white-space:pre">		</span>pBegin++, pEnd--;
<span style="white-space:pre">	</span>}
}
int main()
{
char s[] = "abcdefg";
LeftReverse(s,6);
cout << s << endl;
return 0;
}
先分别反转,后全反转

char* LeftRotateString(char* pStr,int n){

int len=strlen(pStr);

if(pStr==NULL || n<=0 || n>=len)

return pStr;

char* pFirstStart=pStr;

char* pFirstEnd=pStr+n-1;

char* pSecondStart=pStr+n;

char* pSecondEnd=pStr+len-1;

Reverse(pFirstStart,pFirstEnd);

Reverse(pSecondStart,pSecondEnd);

Reverse(pFirstStart,pSecondEnd);

return pStr;

}


题目2:

输入一个英文句子,反转句子中单词的顺序,但单词内字符的顺序不变。为简单起见,标点和普通字母一样处理。例如输入字符串“I am a student.”,则输出“student. a am I”
解题思路,先反转整个句子,在反转每个大慈中字符的顺序

(1) 反转整个句子 得到 “.tneduts a ma I  ”

(2)反转每个单词   “student. a am I”

代码

void Reverse(char *pBegin, char *pEnd)
{
if (pBegin == NULL || pEnd == NULL)
return;

while (pBegin < pEnd)
{
char temp = *pBegin;
*pBegin = *pEnd;
*pEnd = temp;

pBegin++, pEnd--;
}
}
void ReverseSentence(char *pData)
{
if (pData == NULL)
return ;

char *pBegin = pData;

char *pEnd = pData;
while (*pEnd != '\0')
pEnd++;
pEnd--;

// 翻转整个句子
Reverse(pBegin, pEnd);

// 翻转句子中的每个单词
pBegin = pEnd = pData;
while (*pBegin != '\0')
{
if (*pBegin == ' ')
{
pBegin++;
pEnd++;
}
else if (*pEnd == ' ' || *pEnd == '\0')
{
Reverse(pBegin, --pEnd);
pBegin = ++pEnd;
}
else
{
pEnd++;
}
}
}

题目三


Text Reverse

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 26601    Accepted Submission(s): 10364


Problem Description

Ignatius likes to write words in reverse way. Given a single line of text which is written by Ignatius, you should reverse all the words and then output them.

 

Input

The input contains several test cases. The first line of the input is a single integer T which is the number of test cases. T test cases follow.

Each test case contains a single line with several words. There will be at most 1000 characters in a line.

 

Output

For each test case, you should output the text which is processed.

 

Sample Input

3
olleh !dlrow
m'I morf .udh
I ekil .mca

 

Sample Output

hello world!
I'm from hdu.
I like acm.

Hint
Remember to use getchar() to read '\n' after the interger T, then you may use gets() to read a line and process it.

 

代码:

int main()
{
string input;
int n,length,count=0;
cin >> n;
getchar();
for (int i = 0; i < n; i++)
{
getline(cin,input);
length = input.size();
input[length] = ' ';
for (int j = 0; j <=input.size(); j++)
{
if (input[j] != ' ' )
count++;
else
{
for (int z = j - 1; z >= j - count; z--)
cout << input[z];
if (j != length)
cout << " ";
count = 0;
}
}
cout << endl;
}
retur
4000
n 0;
}


题目四    句子逆序

参与人数:2418时间限制:1秒空间限制:32768K

本题知识点: 数组

 算法知识视频讲解


题目描述

将一个英文语句以单词为单位逆序排放。例如“I am a boy”,逆序排放后为“boy a am I”

所有单词之间用一个空格隔开,语句中除了英文字母外,不再包含其他字符

接口说明

/**

 * 反转句子

 * 

 * @param sentence 原句子

 * @return 反转后的句子

 */

public String reverse(String sentence);

 

 

 

输入描述:
将一个英文语句以单词为单位逆序排放。

输出描述:
得到逆序的句子

输入例子:
I am a boy


输出例子:
boy a am I


void rev(string &s,int a,int b)
{
for (int i = a, j =b; i <j; i++, j--)
{
char temp = s[i];
s[i] = s[j];
s[j] = temp;
}
}
int main()
{
string s;
getline(cin,s);
rev(s,0,s.size()-1);
int a=0, b=0;
while (b< s.size())
{
if (s[a] == ' ')
{
a++;
b++;
}
else if (s[b] == ' ')
{
rev(s, a, b - 1);
a = b++;
}
else
b++;
}
rev(s, a, b - 1);
cout << s<<endl;
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: