字符串左旋转&&反转单词顺序&&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; }
相关文章推荐
- 在本地开global_names=true时,如何建db_link访问远端的数据库。
- 第8章 自己写库-构建库函数雏形—零死角玩转STM32-F429系列
- 前后端分离
- linux系统更改目录和文件的权限总结
- 表有外键所以delete报错了,这里有2种办法处理:
- canvas中圆的角度和弧度
- 《Head First设计模式》学习总结
- 电脑疑难杂症
- Java多线程中Sleep与Wait的区别?
- Hust oj 1631 技能修炼(拓扑排序)
- POJ 1088 滑雪
- Redis常用监控和优化方案
- POJ-1251-Jungle Roads(克鲁斯卡尔)
- POJ1017 【据说是贪心...】
- Sql plus命令报command not found的解决笔记
- LeetCode 16. 3Sum Closest
- 0x7FFFFFFF 是long int的最大值
- 【Bash百宝箱】repo init error - manifest xxx.xml not available
- Oracle 启动实例(instance)、打开数据库
- Python 学习笔记(Machine Learning In Action)K-近邻算法(KNN)机器学习实战