Reading the entire input char by char can be done quickly this way: while ((input = (char)getchar())) { if (input == EOF) { break; } The canonical way is: while ((input = std::getc()) != EOF) { ... } Check if your input correctly outputs the last word, if the last character is latin. The first test ends with a . Edited by author 21.03.2023 18:29 #include <iostream> #include <vector> #include <cmath> #include <string> #include <stack> using namespace std; string solve(string text) { string ts, cs = "", ans = ""; int i = 0; while (i < text.size()) { while(int(text[i]) >= 65 && int(text[i]) <= 122) { ts = ""; // create temp string ts.push_back(text[i]); // translate char to string cs.insert(0, ts); //reverse string i++; } if (i > 0 && int(text[i-1]) >= 65 && int(text[i-1]) <= 122) { ans += cs; //plus to out ans cypher word cs = ""; // make cypher word "" }
ans+=text[i]; i++; } ans += char(0x20); ans += char(0x0a); return ans; } int main() { string s; while(getline(cin, s)) cout << solve(s); } #include <iostream> #include <string> #include <algorithm> using namespace std; int main() { string c = "a"; string word; for (;;) { c = getc(stdin); if (c[0] == EOF) { reverse(word.begin(), word.end()); cout << word; break; } if (((c[0] >= 'a') && (c[0] <= 'z')) || ((c[0] >= 'A') && (c[0] <= 'Z'))) { word.append(c); } else { reverse(word.begin(), word.end()); cout << word << c; word.clear(); } } } from sys import stdin s = "" for line in stdin: for i in line: if 'A' <= i <= 'Z' or 'a' <= i <= 'z': s += i elif 9 < ord(i) < 14 or ord(i) < 0: # <-- this elif about non-printable print(s[::-1], end = '\n') # characters end of lines, files, etc. s = "" else: print(s[::-1] + i, end = '') s = "" print(s, end = '') I think I can not get AC because of "Enter" key on Windows is "\r\n" and C++ scanf() reads it as "\n" and printf() writes "\n" as "\r\n". So, if in input is some "\n", then I would read it as "\n" and then print it as "\r\n"!!! I don't know how to fix that. Also there were problems with EOF-character. Yeah, I have the same problem! My ass is burning after solving this problem The test is empty. There is nothing there. I really don't understand why my solution is wrong but here it is: letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" start = 0 s = input() for i in range(0, len(s)): if not(s[i] in letters): word = s[start:i] other = s[i:i+1] if (word != ""): print(word[::-1], end="")
print(other, end="") start = i+1 if (start != len(s)): print(s[start:len(s)][::-1], end="") What's wrong with this solution? WA (Test 2) Edited by author 30.03.2013 23:10 I think that the input can be more than one line. For those looking for a hint: I use stack. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Push the characters into stack, when a non-alphabetic character is read, pop out all the characters in stack and print them before printing the non-alphabetic character. Edited by author 02.10.2018 15:04 #include <bits/stdc++.h> using namespace std; #define re return #define pb push_back #define all(x) (x).begin(), (x).end() #define fi first #define se second #define sqrt(x) sqrt(abs(x)) #define mp make_pair #define pi (3.14159265358979323846264338327950288419716939937510) #define fo(i, n) for(int i = 0; i < n; ++i) #define ro(i, n) for(int i = n - 1; i >= 0; --i) #define unique(v) v.resize(unique(all(v)) - v.begin()) template <class T> T abs (T x) { re x > 0 ? x : -x; } template <class T> T sqr (T x) { re x * x; } template <class T> T gcd (T a, T b) { re a ? gcd (b % a, a) : b; } template <class T> int sgn (T x) { re x > 0 ? 1 : (x < 0 ? -1 : 0); } typedef vector<int> vi; typedef vector<vi> vvi; typedef pair<int, int> ii; typedef vector<ii> vii; typedef vector<string> vs; typedef double D; typedef long double ld; typedef long long ll; typedef pair<ll, ll> pll; typedef vector<ll> vll; typedef unsigned long long ull; vi l, r; int main() { //freopen("input.txt", "r", stdin); string str; while (getline(cin, str)) { l.clear(), r.clear(); if (isalpha(str[0])) l.pb(0); fo(i, (int)str.size() - 1) { if (!isalpha(str[i]) && isalpha(str[i + 1])) l.pb(i + 1); if (isalpha(str[i]) && !isalpha(str[i + 1])) r.pb(i + 1); } if (isalpha(str.back())) r.pb(str.size()); fo(i, l.size()) reverse(str.begin() + l[i], str.begin() + r[i]); cout << str << '\n'; str.clear(); } } Here is my code which has been written in java. Please help me. I cannot understand what wrong is. Or some tests you have ?. Thanks. public class EsreveRredrO_1226 { public static void main(String[] args) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); String s = reader.readLine(); solve(s); } public static void solve(String s) { String text[]; text = s.split(" "); for (int i = 0; i < text.length; i++) { String revers = ""; String temp; for (int j = text[i].length() - 1; j >= 0; j--) { revers = revers.concat(String.valueOf(text[i].charAt(j))); } if (revers.charAt(0) == '!' || revers.charAt(0) == ',' || revers.charAt(0) == '.' || revers.charAt(0) == '?' || revers.charAt(0) == ':' || revers.charAt(0) == ' ') { temp = revers.substring(1, revers.length()); revers = temp.concat(String.valueOf(revers.charAt(0))); } if (i != text.length - 1) System.out.print(revers + " "); else System.out.print(revers); } } } Edited by author 04.09.2017 00:45 I remember the problem is quite a pain in the ass I remember in that problem you should pay attention on all symbols If there is new line \n\r you print so If just \n you print \n If there is no EOF you don't print EOF I have not read your code I have WA2 for a long time And those symbols at the end of the line were related to those WA's Thanks for your words. I did care each symbol and didn't print any additional character at the end, and got AC. :) #include <iostream> #include <iomanip> #include <map> #include <vector> #include <cmath> #include <vector> #include <algorithm> #include <cmath> using namespace std; int letter(char c) { return (('a'<=c&&c<='z')||('A'<=c&&c<='Z')); } int main() { //freopen("a","r",stdin); string s; while(getline(cin,s)) { vector<int> l,r; for (size_t i=0;i<s.length();i++) { if (letter(s[i])) { if (!i||!letter(s[i-1])) { l.push_back(i); } if (i+1==s.length()||!letter(s[i+1])) { r.push_back(i+1); } } } for (int i=0;i<l.size();i++) { reverse(s.begin()+l[i],s.begin()+r[i]); } cout << s << endl; } return 0; } import sys def reverse(i): if len(i) == 1: return i else: return i[len(i)-1]+reverse(i[:len(i)-1]) def getReverseLine(s): start = 0 for i in range(len(s)): if (ord(s[i]) < 123 and ord(s[i]) > 64) and (ord(s[i]) < 91 or ord(s[i]) > 96): if i == len(s)-1: if start < i: s = s.replace(s[start:i+1], reverse(s[start:i+1])) else: if start < i: s = s.replace(s[start:i], reverse(s[start:i])) start = i+1 else: start += 1 return s lines = [] while True: line = sys.stdin.read() if line == '': break lines.append(getReverseLine(line)) text = '\n'.join(lines) print text #include <bits/stdc++.h> using namespace std; string a,b; string re(long long c, long long d,string x){ string y=""; for(long long i=d; i>=c; i--) y=y+x[i]; return y; } string re2(string x){ long long t=0; string res=""; for(long long i=0; i<x.size(); i++){ int q=(int)x[i]; if(q<65||(q>90&&q<97)||q>122){ res=res+re(t,i-1,x); res=res+x[i]; t=i+1; } } res=res+re(t,x.size()-1,x); return res; } int main() { while(getline(cin,a)){ istringstream iss; iss.clear(); iss.str(a); while(iss>>b) cout<<re2(b)<<" "; cout<<endl; } } In the end you write empty line. You must delete your last cout. Thanks you. But I repaired and still WA#1 Try next tests(be attentive to the newline and whitespace): 1) asdad asd sf dv answer: dadsa dsa fs vd 2)1 2 3 $ asd#!@#sad%$#%2 sdSA d!23!`123?<M@! ^^&(&*^&as asf kljdka jd1892u31ASDASD11 end. answer: 1 2 3 $ dsa#!@#das%$#%2 ASds d!23!`123?<M@! ^^&(&*^&sa fsa akdjlk dj1892u31DSADSA11 dne. 3)You must inverse word and all. But y ou could forget about the spaces and line breaks ! answer: uoY tsum esrevni drow dna lla. tuB y uo dluoc tegrof tuoba eht secaps dna enil skaerb ! Oh, I failed in test 1 and then got AC. Tks u so much ^^ Edited by author 05.02.2017 21:40 python 3 my code : import sys for t in sys.stdin : s = '' for i in range(len(t)): if (ord(t[i]) >=97 and ord(t[i]) <= 122) or (ord(t[i])>=65 and ord(t[i])<=90): s += t[i] else : if len(s) > 0 : print(s[len(s)-1:0:-1]+s[0], end='') s = '' print(t[i], end='') print(s, end='') Edited by author 29.08.2016 01:57 All symbols are important, not "only printable". Pay attention to EOL, EOF, and symbols with codes below 32. what is EOL, EOF symbols??????? "end of line" (ascii 13 and 10) and "end of file" here is my code: I don't know wht's the error ,anyone help me? Const u=['a'..'z']+['A'..'Z']; Var s:string; ch:char; i:integer; Begin read(ch); while not eof do begin s:=''; while (ch in u) and (ord(ch)<>26) do begin s:=s+ch; read(ch); end; for i:=length(s) downto 1 do write(s[i]); while (not (ch in u)) and (ord(ch)<>26) do begin write(ch); read(ch); end; end; End. check whether you output EOF. If you output it you will get WA. I got really angry when I understood that my program didn't work, because I print EOF!!!! #include<iostream> #include<algorithm> #include<string> using namespace std; int main() { string text; while(getline(cin,text)) { string s=""; for(int i=0;i<text.size();i++) if(('a'<=text[i]&&text[i]<='z') ||('A'<=text[i]&&text[i]<='Z')) s.push_back(text[i]); else { reverse(s.begin(),s.end()); cout<<s; s=""; cout<<text[i]; } cout<<endl; } //system("pause"); return 0; } import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; public class T1226_esreveR_redrO { public static void main(String[] args) throws NumberFormatException, IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in, "ISO-8859-1")); PrintWriter writer = new PrintWriter(new OutputStreamWriter(System.out, "ISO-8859-1")); String line = ""; while ((line = reader.readLine()) != null) { String[] lineSplit = line.split(" "); reverse(lineSplit); String lineReversed = ""; for (int i = 0; i < lineSplit.length - 1; i++) { lineReversed = lineReversed + lineSplit[i] + " "; } lineReversed = lineReversed + lineSplit[lineSplit.length - 1]; writer.println(lineReversed); } writer.flush(); } private static void reverse(String[] lineSplit) { for (int i = 0; i < lineSplit.length; i++) { String head = ""; String tail = ""; String word = lineSplit[i]; if (!Character.isLetter(word.charAt(0))) { head = word.charAt(0) + ""; word = word.substring(1, word.length()); } if (!Character.isLetter(word.charAt(word.length() - 1))) { tail = word.charAt(word.length() - 1) + ""; word = word.substring(0, word.length() - 1); } StringBuffer sb = new StringBuffer(word); sb = sb.reverse(); word = head + sb.toString() + tail; lineSplit[i] = word; } } } The output text must be exactly as it was given, without any additional new lines (but with all new lines that were in input data). |
|