九度oj 1525 子串逆序打印
原题链接:http://ac.jobdu.com/problem.php?pid=1525
字符串简单题,注意开有结尾有空格的情况否则pe or wa
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<algorithm> 2 #include<iostream> 3 #include<cstdlib> 4 #include<cstring> 5 #include<cstdio> 6 #include<string> 7 #include<vector> 8 using std::string; 9 using std::vector; 10 using std::cin; 11 const int Max_N = 100010; 12 char buf[Max_N], temp[Max_N]; 13 void reverse(char *src, int n){ 14 int i, j = n - 1; 15 for (i = 0; i < j; i++, j--){ 16 char tp = src[i]; 17 src[i] = src[j]; 18 src[j] = tp; 19 } 20 } 21 void solve(int n){ 22 int i, j; 23 vector<string> ans; 24 for (i = 0; i < n; i++){ 25 if (buf[i] != ' ' || !i){ 26 j = 0; 27 for (; buf[i] != ' ' && i < n; i++) temp[j++] = buf[i]; 28 temp[j] = '\0'; 29 reverse(temp, j); 30 ans.push_back(temp); 31 } 32 } 33 if (buf[n - 1] == ' ') ans.push_back(""); 34 int t = ans.size(); 35 for (i = 0; i < t; i++){ 36 printf("%s%c", ans[i].c_str(), i < t - 1 ? ' ' : '\n'); 37 } 38 } 39 int main(){ 40 #ifdef LOCAL 41 freopen("in.txt", "r", stdin); 42 freopen("out.txt", "w+", stdout); 43 #endif 44 int n; 45 while (~scanf("%d", &n) && n){ 46 while (getchar() != '\n'); 47 gets(buf); 48 solve(n); 49 } 50 return 0; 51 }
By: GadyPu 博客地址:http://www.cnblogs.com/GadyPu/ 转载请说明