elttiL moT nwod eht teerts sllac ruo god ” ehT peek god ” . piZ si a peehs god . tuB nehw moT seirt ot yas ” peeS ” , ti semoc tuo ” peek ” . dnA ni a yaw moT si thgir . piZ si syawla gnignirb sgniht oh rof su ot peek ! ll’I llet uoy tuoba emos fo meht .
s’piZ tsrif tneserp saw a eohs . tI saw edam fo neerg klis .
eW t’ndid wonk woh piZ dnuof eht eohs . tuB retfa a tnemom yraM , ym gib retsis , dlot em eht eohs dah a egnarts llems . I deddon dna dleh ym eson . ” tahW od uoy kniht ti si ? ”
” tI sllems ekil gnihtemos rof gninaelc . I kniht enoemos deirt ot naelc a tops ffo eht eohs . nehT eh tup ti ta eht rood ot yrd . ”
” gnolA emac piZ . dnA eyb-doog eohs ! ” I dias . ” eW dluohs ekat ti kcab . ”
” eW t’nac ” . dias ym rettsis .
” ebyaM elttil moT si thgir , ” yraM dias . ” ebyaM piZ si a peek god ! “
你正在做英語閱讀,可哪知這是一篇咸魚文章,整個文章的所有單詞都是翻轉的,你很慌。
不過你是咸魚程序員,你可以寫代碼將這篇文章翻轉回來,那么翻轉回來吧。
INPUT
輸入一篇英文文章。
輸入數據中只包含空格、換行符和小寫大寫字母。
滿足總字數小于等于100000
OUTPUT
你應該把這個文章的所有單詞都翻轉回來,輸出即可。
SAMPLE INPUT
AAA BBB
AB AB
SAMPLE OUTPUT
AAA BBB
BA BA
sb題搞死我,氣死我啦!!!
剛開始在想空格與換行的區分(其實不用區分),想它們的碼值。
后來看來題解之后
題解是這樣的:F 咸魚文章
用棧來做這道題,遇到字母,我們就扔到棧里面去,遇到空格或者換行的時候,我們就把棧里面的元素輸出,然后再輸出空格/換行就好了。
然后我就超時了,超時代碼附上
//超時代碼,兩層for,gg
#include<cstdio>
#include<cstring>
#include<string>
#include<queue>
#include<stack>
#include<iostream>
#include<algorithm>
using namespace std;
const int inf=0x3f3f3f3f;
char str[10000][10000];
int main()
{int len=0;memset(str,0,sizeof(str));while(gets(str[len++])){if(str[len-1][0]==0)break;}for(int i=0;i<len-1;i++){stack<char>q;int p=strlen(str[i]);for(int j=0;j<=p;j++){//printf("%d ",j);if((str[i][j]>='a'&&str[i][j]<='z')||(str[i][j]>='A'&&str[i][j]<='Z')){q.push(str[i][j]);}else{while(!q.empty()){printf("%c",q.top());q.pop();}printf(" ");}}printf("\n");}return 0;
}
大概看了別人代碼后,又想怎么結束輸入(不用想,Ctrl+z,結束輸入,水題刷的不夠啊!)
然后粘正確代碼
#include<cstdio>
#include<cstring>
#include<stack>
#include<string>
#include<iostream>
#include<algorithm>
using namespace std;
char a[100005];
char b[100005];
int main()
{int perlen=0;while(gets(a)){int p=0;int len=strlen(a);//長度正好包含最后一個字母,沒有'\n'for(int i=perlen; i<perlen+len; i++)b[i]=a[p++];b[perlen+len]='\n';perlen=perlen+len+1;}stack<char>q;for(int i=0; i<perlen; i++){if((b[i]>='a'&&b[i]<='z')||(b[i]>='A'&&b[i]<='Z'))q.push(b[i]);else{while(!q.empty()){printf("%c",q.top());q.pop();}printf("%c",b[i]);}}return 0;
}
1.不用考慮輸入終止
2.學習這種記錄空格與換行
3.strlen()的長度是到最后一個字母的長度,沒有’\n’