C++_USACO_Broken Necklace
2013-07-18 11:02
288 查看
/* PROB:beads LANG:C++ */ #include<iostream> #include<string> #include <cstdlib> #include <cstring> #include<fstream> using namespace std; string strReverse(string str1){ int len=str1.size(); string str2=str1; char *end=&str1[0]+len-1; for(int i=0;i<len;i++) if(end>&str1[0]){ str2[i]=end[0]; end--; } else if(end==&str1[0]){ str2[i]=end[0]; break; } return str2; } int first_maxLen(string newstr){ int i=0; int n=newstr.size(); int temp_len=0; char br=' '; if(newstr[0]=='w'){ while(i<n){ if(newstr[i]=='w' || newstr[i]==br){ temp_len++; i++; continue; } if(br==' '){ br=newstr[i]; temp_len++; i++; continue; } if(br!=newstr[i]) break; } } else{ br=newstr[0]; while(i<n){ if(newstr[i]==br||newstr[i]=='w'){ temp_len++; i++; continue; } else break; } } return temp_len; } int main(){ ifstream ifs("beads.in"); ofstream ofs("beads.out"); int n; int back_len; int forward_len; int new1_firstmax; int new2_firstmax; int new1_finalmax=0; int new2_finalmax=0; int max=0; int tempmax=0; string necklace=""; ifs>>n; if(n>350||n<3) return 0; ifs>>necklace; string reverse_lace=strReverse(necklace); new1_finalmax=first_maxLen(necklace); new2_finalmax=first_maxLen(reverse_lace); if(reverse_lace.compare(necklace)==0){ max=new1_finalmax; } else{ max=new1_finalmax+new2_finalmax; } string back_str=""; string forward_str=""; string newstr1=""; string newstr2=""; for(int i=0;i<n-1;i++){ back_str=&necklace[i+1]; back_len=back_str.size(); forward_len=n-back_len; forward_str=necklace.substr(0,forward_len); newstr1=strcat(&back_str[0],&forward_str[0]); newstr2=strReverse(newstr1); new1_firstmax=first_maxLen(newstr1); new2_firstmax=first_maxLen(newstr2); if(newstr1.compare(newstr2)==0) tempmax=new1_firstmax; else{ tempmax=new1_firstmax+new2_firstmax; } if(tempmax>max) max=tempmax; } ofs<<max<<endl; return 0; }
代码质量很差,用了很多字符串拷贝,占用过多内存。而且自己电脑上面测试都没有错,但是结果在USACO上面总是错误的。还未解决!可以参考给的答案。发现人家的答案好简单!对比一下。
我的错误如下:
Compiling... Compile: OK
Executing... Test 1: TEST OK [0.000 secs, 3500 KB] Test 2: TEST OK [0.000 secs, 3500 KB] Test 3: TEST OK [0.000 secs, 3500 KB] Test 4: TEST OK [0.000 secs, 3500 KB] Test 5: TEST OK [0.000 secs, 3500 KB]
> Run 6: Execution error: Your program had this runtime error: Illegal file open (/dev/tty). The program ran for 0.000 CPU seconds before the error. It used 3500 KB of memory. ------ Data for Run 6 [length=11 bytes] ------ 8 rrwwwwbb ---------------------------- |
Test 6: SIGNAL 50 (0.000 secs, 3500 KB)
人家的代码哦:
#include<fstream> using namespace std; int main() { int num,max=0,n,i,j,k; char *p,symbol1,symbol2; ifstream fin("beads.in"); ofstream fout("beads.out"); fin>>n; p=new char[n+1]; fin>>p; for(i=n;i<2*n;++i){ if(p[i%n]!=p[(i+1)%n]){ symbol1=p[i%n]!='w'?p[i%n]:(p[(i+1)%n]=='r'?'b':'r'); symbol2=symbol1=='r'?'b':'r'; num=0; for(j=i;j>i-n;--j){ if(p[j%n]==symbol1||p[j%n]=='w') ++num; else break; } for(k=i+1;k<j+n+1;++k){ if(p[k%n]==symbol2||p[k%n]=='w') ++num; else break; } max=max>num?max:num; } } if(max==0) max=n; fout<<max<<endl; fin.close(); fout.close(); delete [] p; return 0; }
相关文章推荐
- [USACO1.1]Broken Necklace C++(错误1)
- USACO-Section1.1 Broken Necklace [动态规划]
- USACO1.1 Broken Necklace 比较分析
- usaco Broken Necklace字符串
- USACO Section 1.1 Broken Necklace 解题报告
- usaco 1.1 Broken Necklace
- Broken Necklace(USACO)
- USACO<s1.1.4:Broken Necklace>字符串
- [USACO] Broken Necklace
- [USACO1.1]坏掉的项链Broken Necklace
- USACO 1.1 Broken Necklace (模拟)
- Usaco 1.1.4 破碎的项链(Broken Necklace)
- 【USACO1.1_4】★Broken Necklace 破碎的项链
- USACO 1.1.4 Broken Necklace
- Broken Necklace-----破碎的项链----USACO---C1S1
- 【USACO-Chapter1-1.1】【DP】Broken Necklace
- 【P1203】 【USACO1.1】坏掉的项链Broken Necklace
- Broken Necklace(USACO)
- USACO 1.1.4 Broken Necklace(模拟)
- USACO 1.1 Broken Necklace