两道题目都是很简单的题目,训练计划里这两道题目是说用c++标准模板做的。好吧,我承认我对c++标准模板库函数一无所知。不过看别人说的,如果数据量够大的话,用模板还是很好的,但是如果数据量不是那么大,用模板好像总是比自己写的慢。
3007 http://poj.org/problem?id=3007 今天早上做的时候,去网上搜了一下set怎么用,然后写出代码了,但是果断TLE,看网上的标程好像很多是用hash做的,上午准备用暴搜,可是有点困,没写出来,刚刚用暴搜做了一下,235ms,估计是后台数据不强吧,要不肯定要挂掉的
题意就是:给你一段字符串,重任意位置切开,成两端,再把这两段分别逆序,这样就可以得到四段,然后这四段组合,看一共能组合出多少种不同的字符串
View Code
1 #include <stdio.h> 2 #include <iostream> 3 #include <string.h> 4 #include <algorithm> 5 #define N 10000 6 #define M 80 7 8 using namespace std; 9 10 char sn[4][M],zs[N][M]; 11 char str[M],zh[8][M]; 12 int n,cnt; 13 void find(int x,int len) 14 { 15 int i,j,h; 16 for(i = x,h = 0; i >= 0; i--,h++) 17 sn[1][h] = str[i]; 18 sn[1][h] = '\0'; 19 for(i = len - 1,h = 0; i > x; i--,h++) 20 sn[3][h] = str[i]; 21 sn[3][h] = '\0'; 22 int len1,len2,temp,kemp; 23 len1 = strlen(sn[0]),len2 = strlen(sn[2]); 24 temp = kemp = 0; 25 while(kemp < len1) 26 { 27 zh[0][temp ++] = sn[0][kemp ++]; 28 } 29 kemp = 0; 30 while(kemp < len2) 31 { 32 zh[0][temp ++] = sn[2][kemp ++]; 33 } 34 35 len1 = strlen(sn[0]),len2 = strlen(sn[3]); 36 temp = kemp = 0; 37 while(kemp < len1) 38 { 39 zh[1][temp ++] = sn[0][kemp ++]; 40 } 41 kemp = 0; 42 while(kemp < len2) 43 { 44 zh[1][temp ++] = sn[3][kemp ++]; 45 } 46 47 len1 = strlen(sn[1]),len2 = strlen(sn[2]); 48 temp = kemp = 0; 49 while(kemp < len1) 50 { 51 zh[2][temp ++] = sn[1][kemp ++]; 52 } 53 kemp = 0; 54 while(kemp < len2) 55 { 56 zh[2][temp ++] = sn[2][kemp ++]; 57 } 58 59 len1 = strlen(sn[1]),len2 = strlen(sn[3]); 60 temp = kemp = 0; 61 while(kemp < len1) 62 { 63 zh[3][temp ++] = sn[1][kemp ++]; 64 } 65 kemp = 0; 66 while(kemp < len2) 67 { 68 zh[3][temp ++] = sn[3][kemp ++]; 69 } 70 71 len1 = strlen(sn[2]),len2 = strlen(sn[0]); 72 temp = kemp = 0; 73 while(kemp < len1) 74 { 75 zh[4][temp ++] = sn[2][kemp ++]; 76 } 77 kemp = 0; 78 while(kemp < len2) 79 { 80 zh[4][temp ++] = sn[0][kemp ++]; 81 } 82 83 len1 = strlen(sn[2]),len2 = strlen(sn[1]); 84 temp = kemp = 0; 85 while(kemp < len1) 86 { 87 zh[5][temp ++] = sn[2][kemp ++]; 88 } 89 kemp = 0; 90 while(kemp < len2) 91 { 92 zh[5][temp ++] = sn[1][kemp ++]; 93 } 94 95 len1 = strlen(sn[3]),len2 = strlen(sn[0]); 96 temp = kemp = 0; 97 while(kemp < len1) 98 { 99 zh[6][temp ++] = sn[3][kemp ++]; 100 } 101 kemp = 0; 102 while(kemp < len2) 103 { 104 zh[6][temp ++] = sn[0][kemp ++]; 105 } 106 107 len1 = strlen(sn[3]),len2 = strlen(sn[1]); 108 temp = kemp = 0; 109 while(kemp < len1) 110 { 111 zh[7][temp ++] = sn[3][kemp ++]; 112 } 113 kemp = 0; 114 while(kemp < len2) 115 { 116 zh[7][temp ++] = sn[1][kemp ++]; 117 } 118 for(i = 0; i < 8; i++) 119 { 120 for(j = 0; j < cnt; j++) 121 if(!strcmp(zs[j],zh[i])) break; 122 if(j == cnt) strcpy(zs[cnt++],zh[i]); 123 } 124 } 125 int main() 126 { 127 int i,j,k,h; 128 //freopen("data.txt","r",stdin); 129 while(cin>>n) 130 { 131 for(i = 0; i < n; i++) 132 { 133 memset(zs,0,sizeof(zs)); 134 cnt = 0; 135 cin>>str; 136 int len = strlen(str); 137 for(j = 0; j < len; j++) 138 { 139 memset(sn,0,sizeof(sn)); 140 memset(zh,0,sizeof(zh)); 141 for(k = 0; k <= j; k++) 142 sn[0][k] = str[k]; 143 sn[0][k] = '\0'; 144 for(k = j + 1,h = 0; k < len; k++,h++) 145 sn[2][h] = str[k]; 146 sn[2][h] = '\0'; 147 find(j,len); 148 } 149 cout<<cnt<<endl; 150 } 151 } 152 return 0; 153 }
同样是暴搜的,不过也用set做了一下
http://poj.org/problem?id=3096
题意就不说了,仔细读题都能读懂,也没有任何注意的地方
暴搜代码 和set代码
View Code
1 #include <stdio.h> 2 #include <iostream> 3 #include <math.h> 4 #include <string.h> 5 #define N 80 6 7 using namespace std; 8 9 char str[N],sbr[N][3]; 10 int find(int x,int len) 11 { 12 int i,k,j,h; 13 j = 0; 14 for(i = 0; i + x < len; i++) 15 { 16 sbr[j][0] = str[i]; 17 sbr[j][1] = str[i + x]; 18 sbr[j][2] = '\0'; 19 j++; 20 } 21 /*for(i = 0; i < j; i++) 22 cout<<sbr[i]<<" "; 23 cout<<endl;*/ 24 int flag = 0; 25 for(i = 0; i < j; i++) 26 { 27 for(k = i + 1; k < j; k++) 28 if(!strcmp(sbr[i],sbr[k])) 29 { 30 flag = 1;break; 31 } 32 } 33 if(flag) return 0; 34 else return 1; 35 } 36 int main() 37 { 38 int i; 39 int len; 40 //freopen("data.txt","r",stdin); 41 while(cin>>str,strcmp(str,"*")) 42 { 43 len = strlen(str); 44 int mark = 0; 45 for(i = 0; i < len; i++) 46 { 47 if(!find(i + 1,len)) 48 { 49 mark = 1;break; 50 } 51 } 52 if(!mark) printf("%s is surprising.\n",str); 53 else printf("%s is NOT surprising.\n",str); 54 } 55 return 0; 56 }
View Code
1 #include <stdio.h> 2 #include <iostream> 3 #include <math.h> 4 #include <string.h> 5 #include <set> 6 #include <string> 7 #define N 80 8 9 using namespace std; 10 11 int main() 12 { 13 int i,j; 14 string str; 15 //freopen("data.txt","r",stdin); 16 while(cin>>str,str != "*") 17 { 18 string sbr; 19 set<string>scr; 20 int flag = 0; 21 for(i = 1; i < str.size(); i++) 22 { 23 scr.clear(); 24 for(j = 0; j + i < str.size(); j++) 25 { 26 sbr.clear(); 27 sbr += str[j]; 28 sbr += str[j + i]; 29 scr.insert(sbr); 30 } 31 if(scr.size() != str.size() - i) 32 { 33 flag = 1; 34 break; 35 } 36 } 37 if(!flag) cout<<str<<" is surprising.\n";//printf("%s is surprising.\n",str); 38 else cout<<str<<" is NOT surprising.\n";//else printf("%s is NOT surprising.\n",str); 39 } 40 return 0; 41 }