POJ 3007.Organize Your Train part II

来源:互联网 发布:数据结构和算法分析pdf 编辑:程序博客网 时间:2024/06/05 16:14

题目:http://poj.org/problem?id=3007

AC代码(C++):

#include <iostream>#include <queue>#include <map>#include <string>#include <string.h>#include <algorithm>#define INF 0x3f3f3f3fusing namespace std;int cnt;char strs[1000][80];bool isExist(char* str) {for (int i = 0; i < cnt; i++) {if (strcmp(str, strs[i])==0)return true;}return false;}int main() {int t;cin >> t;char input[80];char str1[80];char str2[80];char str3[80];char str4[80];char str11[80];char str22[80];char str33[80];char str44[80];char str55[80];char str66[80];char str77[80];char str88[80];while (t--) {cin >> input;int len = strlen(input);cnt = 0;for (int i = 1; i <= len - 1; i++) {memset(str1,0,sizeof(str1));memset(str2,0,sizeof(str2));strncpy(str1, input, i);strncpy(str2, input + i, strlen(input + i));strcpy(str3, str1);strcpy(str4, str2);reverse(str3, str3 + strlen(str3));reverse(str4, str4 + strlen(str4));strcpy(str11, str1);strcat(str11, str2);strcpy(str22, str1);strcat(str22, str4);strcpy(str33, str3);strcat(str33, str2);strcpy(str44, str3);strcat(str44, str4);strcpy(str55, str2);strcat(str55, str1);strcpy(str66, str2);strcat(str66, str3);strcpy(str77, str4);strcat(str77, str1);strcpy(str88, str4);strcat(str88, str3);if (!isExist(str11)) {strcpy(strs[cnt], str11);cnt++;}if (!isExist(str22)) {strcpy(strs[cnt], str22);cnt++;}if (!isExist(str33)) {strcpy(strs[cnt], str33);cnt++;}if (!isExist(str44)) {strcpy(strs[cnt], str44);cnt++;}if (!isExist(str55)) {strcpy(strs[cnt], str55);cnt++;}if (!isExist(str66)) {strcpy(strs[cnt], str66);cnt++;}if (!isExist(str77)) {strcpy(strs[cnt], str77);cnt++;}if (!isExist(str88)) {strcpy(strs[cnt], str88);cnt++;}}cout << cnt << endl;}}
总结: 不推荐使用STL, 会各种超时, 用char数组直接干就是了.