File Fragmentation-ACM

// File Fragmentation.cpp : Defines the entry point for the console application. // #include "stdafx.h" using namespace std; void getBigSmallSize(vector<string> v,int *bigsize,int *smallsize){ *bigsize=v[0].length(); *smallsize=v[0].length(); for(int i=1;i<v.size();++i){ if(v[i].length()>*bigsize)*bigsize=v[i].length(); if(v[i].length()<*smallsize)*smallsize=v[i].length(); } } bool getString(vector<string> v,string s,int i,int j){ int fileLength=v[i].length()+v[j].length(); int fileNum=v.size()/2; int L=1; vector<int>IsErgodic; for(int z=0;z<v.size();++z){ if(z==i||z==j)IsErgodic.push_back(1); else IsErgodic.push_back(0); } for(int x=0;x<v.size()-1;++x){ if(IsErgodic[x]==0){ for(int y=x+1;y<v.size();++y){ if(IsErgodic[y]==0){ if((v[x].length()+v[y].length())==fileLength){ string t=v[x]+v[y]; if(t==s){ ++L; IsErgodic[x]=1; IsErgodic[y]=1; } else{ t=v[y]+v[x]; if(t==s){ ++L; IsErgodic[x]=1; IsErgodic[y]=1; } } } } } } } vector<int>().swap(IsErgodic); if(L==fileNum)return true; else return false; } string getFile(vector<string> v,int bigsize,int smallsize){ bool IsFind=false; vector<int>BigFra; vector<int>SmallFra; for(int j=0;j<v.size();++j){ if(v[j].length()==bigsize)BigFra.push_back(j); if(v[j].length()==smallsize)SmallFra.push_back(j); } for(int x=0;x<BigFra.size();++x){ for(int y=0;y<SmallFra.size();++y){ string s=v[BigFra[x]]+v[SmallFra[y]]; IsFind=getString(v,s,BigFra[x],SmallFra[y]); if(IsFind==true){ vector<int>().swap(BigFra); vector<int>().swap(SmallFra); //cout<<s<<"/n"; return s; } else{ s=v[SmallFra[y]]+v[BigFra[x]]; IsFind=getString(v,s,BigFra[x],SmallFra[y]); if(IsFind==true){ vector<int>().swap(BigFra); vector<int>().swap(SmallFra); return s; } //else return "Not Find."; } } } return "Not Find"; } int _tmain(int argc, _TCHAR* argv[]) { vector<string>fragmentation; int testround=0; string tmp; cin>>testround; cout<<"/n"; cin.ignore(); while(testround>0){ while(getline(cin,tmp)){ if(tmp==" ")break; fragmentation.push_back(tmp); } int bigsize=0; int smallsize=0; getBigSmallSize(fragmentation,&bigsize,&smallsize); string file=getFile(fragmentation,bigsize,smallsize); //cout<<bigsize<<" "<<smallsize<<"/n"; //for(int i=0;i<fragmentation.size();++i)cout<<fragmentation[i]<<"/n"; cout<<file<<"/n"; fragmentation.clear(); --testround; cout<<"/n"; } vector<string>().swap(fragmentation); system("pause"); return 0; }  

你可能感兴趣的:(File Fragmentation-ACM)