对字符串中的所有单词进行倒排。
说明:
1、构成单词的字符只有26个大写或小写英文字母;
2、非构成单词的字符均视为单词间隔符;
3、要求倒排后的单词间隔符以一个空格表示;如果原字符串中相邻单词间有多个间隔符时,倒排转换后也只允许出现一个空格间隔符;
4、每个单词最长20个字母;
数据范围:字符串长度满足
对字符串中的所有单词进行倒排。
说明:
1、构成单词的字符只有26个大写或小写英文字母;
2、非构成单词的字符均视为单词间隔符;
3、要求倒排后的单词间隔符以一个空格表示;如果原字符串中相邻单词间有多个间隔符时,倒排转换后也只允许出现一个空格间隔符;
输入一行,表示用来倒排的句子
输出句子的倒排结果
I am a student
student a am I
$bo*y gi!r#l
l r gi y bo
#include<string> #include<iostream> #include<vector> #include<algorithm> #include<cstring> #include<cctype> using namespace std; int main() { string str; getline(cin,str); vector<string> vec; int begin = 0; int len = 0; bool renew_begin = true; for(int i = 0; i <str.length(); i++) { if((str[i] >= 'a' && str[i] <= 'z') || (str[i] >= 'A' && str[i] <= 'Z')) { len++; if(renew_begin) { begin = i; renew_begin = false; } if(i == str.length()-1) { string temp = str.substr(begin,len); vec.push_back(temp); break; } } else { if(len>=1) { string temp = str.substr(begin,len); vec.push_back(temp); renew_begin = true; len = 0; } } } reverse(vec.begin(), vec.end()); for(auto it = vec.begin(); it != vec.end(); it++) { if(it<vec.end()-1) cout<<*it<<" "; else cout<<*it; } cout<<endl; return 0; }
while True: try: s = input().split() res = [] for i in s: t = [] i = list(i) for j in i: if j.isalpha(): t.append(j) else: t.append(" ") t = "".join(t).split() res.extend(t) res = res[::-1] for i in res: print(i,end=" ") except: break
#include<bits/stdc++.h> using namespace std; int main() { string x; while(getline(cin,x)) { int i; for(i=0;i<x.size();i++) { if(!((x[i]>='a'&&x[i]<='z')||(x[i]>='A'&&x[i]<='Z'))) x[i]=' ';//判断是不是字母,不是就替换成空格 } string a; while(x.find_last_of(' ')!=-1)//找到最后一个空格,输出之后的字符串 { a=x.substr(x.find_last_of(' ')+1); cout<<a<<" "; x=x.substr(0,x.find_last_of(' ')); } cout<<x<<endl; } return 0; }
import java.io.BufferedReader; import java.io.InputStreamReader; public class Main { public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String s = br.readLine(); StringBuilder sb = new StringBuilder(); int len = s.length(), i = len - 1, j = i+1; while (i > -1) { while (i > -1 && Character.isLetter(s.charAt(i))) i--; sb.append(s, i + 1, j).append(' '); j = i; while (i > -1 && !Character.isLetter(s.charAt(i))) i--; } System.out.println(sb.toString().trim()); } }
#include <iostream> #include <string> #include <vector> using namespace std; // 修改了上述一个答案,测试用例全部通过 int main() { string str; while (getline(cin, str)) { vector<string> array; string temp; // 这里i一定要<=str.size() // 因为走到最后的'\0',才会进入else,将最后一部分单词push到array中 for (int i = 0; i <= str.length(); i++) { if (isalpha(str[i]) && str[i] != '\0') temp += str[i]; else { if (temp == "") continue; array.push_back(temp); temp.clear(); } } for (int i = array.size() - 1; i >= 0; i--) cout << array[i] << ' '; cout << endl; } return 0; }
while True: try: s=list(raw_input().split()) nn="" ll=[] l=[] for i in range(len(s)): ls=list(s[i]) #print(ls) x=len(ls) mm=[] for j in range(x): #print(j) if ls[j].isalpha(): mm.append(ls[j]) else: mm.append(" ") ss="".join(mm) ss.strip() l=list(ss.split()) for i in l: ll.append(i) ll=ll[::-1] print(" ".join(ll)) except: break
#include<iostream> #include<string> #include<algorithm> using namespace std; int main() { string inn; while(getline(cin,inn)) { string str; for(char temp:inn) { if((temp<='Z'&&temp>='A')||(temp<='z'&&temp>='a')) str += temp; else if(str.length()>0&&str.back()!=' ') str += ' '; } // ==================== 注意 ===================== // 末尾可能有空格,要删除,90%的通过率就是这里的问题. // =============================================== if(str.back()==' ') //str.erase(str.end()-1); str.erase(str.length()-1); int start = 0; while(1) { int ind = str.find(' ',start); if(ind>=0) reverse(str.begin()+start,str.begin()+ind); else { reverse(str.begin()+start,str.end()); break; } start = ind+1; } reverse(str.begin(),str.end()); if(str.length()==0) cout<<' '<<endl; else cout<<str<<endl; } return 0; }
#include <iostream> #include <string.h> using namespace std; char str[10000]; char dic[10000][22]; int main() { while(gets(str)) { int flag = 0;//判断是否是第一个空格 int len = strlen(str), it = 0, Word = 0;//单词个数 for(int i=0;i<len;i++) { if((str[i]>='a'&&str[i]<='z') || (str[i]>='A'&&str[i]<='Z')) { flag = 1; dic[Word][it++] = str[i]; } else { if(flag == 1) { dic[Word][it] = '\0'; it = 0; Word++; flag = 0; } } } dic[Word][it] = '\0'; for(int i=Word;i>=0;i--) { if(dic[i][0]!='\0')//他可能出现句首不是字母的情况 { cout<<dic[i]; if(i) cout<<" "; } } cout<<endl; } return 0; }
import java.io.BufferedReader; import java.io.InputStreamReader; public class Main { public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String[] str = br.readLine().split("[^A-Za-z]+"); int length = str.length; StringBuilder sb = new StringBuilder(); for (int i = length - 1;i >= 0;i--){ sb.append(str[i] + " "); } System.out.println(sb.toString().trim()); } }
n=input() for i in range(len(n)): if not n[i].isalpha(): n=n.replace(n[i], ' ') list=n.split(' ') list.reverse() for i in list: print(i,end=' ')
/*** 2/26 一、思路 1.将非大小写字母的char全部替换成空格 注意:只能用replace() 不能用replaceAll() 2.用split(" ")将string分隔进String[] 3.倒序输出 二、匹配非字母的字符进行分割 1.我的思路:将非字母字符全部替换成空格,再用空格分隔 2.其他:正则表达式[^a-zA-Z]分割:String[] words = str.split("[^A-Za-z]"); 三、一些比较: str = str.replace(String.valueOf(c)," ")时,运行时间:134ms,占用内存:17400KB 改为str = str.replace(c,' ')后 运行时间:48ms,占用内存:11548KB,提升30%多 四、知识点:replace() 与 replaceAll() 1.String replace(char oldChar, char newChar) 返回一个新的字符串,它是通过用 newChar 替换此字符串中出现的所有 oldChar 得到的。 2.String replaceAll(String regex, String replacement) 使用给定的 replacement 替换此字符串所有匹配给定的正则表达式的子字符串。 3.replaceAll用的是正则,replace用的是字符,二者都能替换*所有*匹配的字符 示例: String str = "hello.hello.hello"; System.out.println(str.replace(".","#")); //hello#hello#hello System.out.println(str.replaceAll(".","#")); //################# System.out.println(str.replaceAll("\\.","#")); //hello#hello#hello ***/ import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNext()) { String str = sc.nextLine(); char[] chs = str.toCharArray(); for(char c : chs){ if(!Character.isLetter(c)){ str = str.replace(c,' '); } } String[] strArr = str.split(" "); for(int i = strArr.length-1; i>=0; i-- ){ System.out.print(strArr[i] + " "); } } } }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while(sc.hasNextLine()){ String content = sc.nextLine(); content.trim(); char[] ch = content.toCharArray(); String newContent = ""; if(Character.isLetter(ch[0])){ newContent += ch[0]; } for(int i = 1;i < ch.length ; i++){ if(Character.isLetter(ch[i])){ newContent += ch[i]; }else{ if(Character.isLetter(ch[i-1])){ newContent += " "; } } } String[] strArray = newContent.split(" "); for(int i = strArray.length-1;i >= 0;i--){ System.out.print(strArray[i]+" "); } System.out.println(); } } }
package main import ( "bufio" "fmt" "os" ) func main(){ // 读取终端输入 var scanner = bufio.NewScanner(os.Stdin) scanner.Scan() var str = scanner.Text() // 逆序打印,如果非字母只打印空格 var slice []string var tempStr = "" for i:=0;i<len(str);i++ { if (str[i] >= 97 && str[i] <= 122) || (str[i] >= 65 && str[i] <= 90) { tempStr = tempStr + string(str[i]) if i == len(str) -1 { slice = append(slice, tempStr) } }else { slice = append(slice, tempStr) tempStr = "" } } for i:=len(slice)-1;i>=0;i-- { fmt.Printf("%v ",slice[i]) } }