int main()
{
	int n;
	while(cin>>n && n)
	{
		map<string,bool> sw;
		string s,abr;
		for(int i=0;i<n;i++){
			cin>>s;
			sw[s]=1; //mark insignificant words
		}	
		getline(cin,s);
		while(getline(cin,s) && s!="LAST CASE")
		{
			w.clear();
			stringstream ss;
			ss<<s;
			ss>>a;
			abr=a;
			for(int i=0;i<a.size();i++) a[i]=tolower(a[i]);
			while(ss>>s)
				if(sw.find(s)==sw.end())
					w.PB(s);
			memset(dp,-1,sizeof(dp));
			int res=solve(0,0,0);
			if(res==0)
				cout<<abr<<" is not a valid abbreviation"<<endl;
			else
				cout<<abr<<" can be formed in "<<res<<" ways"<<endl;		
		}		
	}	
}
Example #2
0
	//debugging purpose
	void toString(){
		cerr << "Call name: " << call_fun_name << " | Functions: ";
		for(vs::iterator i = function_uses.begin(); i != function_uses.end(); i++){
			cerr << *i << " ";
		}
		cerr << "| Pairs Size " << pairs.size() << endl;
	}
Example #3
0
//----------------------------------------------------------------------------
// Returns true if a given vector<string> contains given string
//----------------------------------------------------------------------------
bool containsElem(vs list, string value) {
	for (vs::iterator it = list.begin(); it != list.end(); it++) {
		if (*it == value) {
			return true;
		}
	}
	return false;
}
Example #4
0
vvb parse_obstacles(const vs &initial_map) {
  vvb obstacles(initial_map.size()+2, vector<bool>(initial_map[0].size()+2, true));
  for (int i = 0; i < initial_map.size(); i++) {
    for (int j = 0; j < initial_map[i].size(); j++) {
      obstacles[i+1][j+1] = (initial_map[i][j] == '*');
    }
  }
  return obstacles;
}
Example #5
0
int main(int argc, char **argv){
	ios_base::sync_with_stdio(0);
	cin.tie(0);	
	
	int t;
	cin>>t;
	string s;
	
	int n;
	bool first=true;
	while(t--){
		if(!first)
			cout<<'\n';
		else first=false;
		cin>>s;
		n=s.size();
		v.clear();
		sum.clear();
		v.resize(n);
		sum.resize(n,vi(n,0));
		v[0]=s;
		for(int i=1;i<n;++i)cin>>v[i];

		for(int i=0;i<n;++i)
			for(int j=0;j<n;++j){
				sum[i][j]=(v[i][j]-'0');
				if(i>0) sum[i][j]+=sum[i-1][j];
				if(j>0) sum[i][j]+=sum[i][j-1];
				if(i>0 && j>0) sum[i][j]-=sum[i-1][j-1];
			}
		
		int largest=0,sumhere;

		/*for(int i=0;i<n;++i)
			for(int j=0;j<n;++j)cout<<sum[i][j]<<(j==n-1?'\n':' ');*/

		for(int i=0;i<n;++i)for(int j=0;j<n;++j)
		for(int p=i;p<n;++p)for(int q=j;q<n;++q){
			//cout<<"("<<i<<","<<j<<") . "<<"("<<p<<","<<q<<")"<<endl;
			sumhere=sum[p][q];
			if(i>0) sumhere-=sum[i-1][q];
			if(j>0) sumhere-=sum[p][j-1];
			if(i>0 && j>0) sumhere+=sum[i-1][j-1];
			if(v[i][j]=='1' && sumhere==(p-i+1)*(q-j+1)) largest=max(largest,sumhere);
		}

		cout<<largest<<'\n';

	}

	return 0;
}
void BuildPath(map<string, vs> &traces, vvs &pathes, vs &path, string word, string start){
    if(word == start){
        path.push_back(word);
        vs tmp = path;
        reverse(tmp.begin(), tmp.end());
        pathes.push_back(tmp);
        path.pop_back();
        return;
    }
    path.push_back(word);
    vs tmp = traces[word];
    for(int i = 0; i < tmp.size(); ++i)
        BuildPath(traces, pathes, path, tmp[i], start);
    path.pop_back();
}
Example #7
0
void print(vs v){
  int n=v.size();
  for(int i=0;i<n; i++){
    cout<<v[i]<<endl;
  }
  cout<<n;
}
Example #8
0
int main(void) {
  std::ios_base::sync_with_stdio (false);
  size_t n;
  cin >> n >> ws;
  vvs graph(n, vs());

  dfs_num.resize(n, 0);
  parent.resize(n, -1);
  finished = false;
  a = b = c = -1;

  string line;
  for (size_t line_cnt = 0; line_cnt < n; line_cnt++) {
    getline(cin, line);
    for (size_t char_cnt = 0; char_cnt < n; char_cnt++) {
      if (line.at(char_cnt) == '1')
	graph[line_cnt].push_back(char_cnt);
    }
  }
  for (size_t counter = 0; counter < n; counter++)
    if (!dfs_num[counter])
      dfs(graph, counter);
  if (a >= 0)
    cout << a+1 << " " << b+1 << " " << c+1 << endl;
  else
    cout << -1 << endl;

  return 0;
}
Example #9
0
 void worker(vs &ret, int level, int pos, string s) {
     if(level == 3) {
         if(s.size() - pos > 3) return;
         else {
             string t = s.substr(pos, 3);
             if(t[0] == '0' && t.size() != 1) return;
             if(stoi(t) <= 255) {
                 ret.push_back(s);
             }
         }
         return;
     } 
     if(s[pos] == '0') {
         string temp = s;
         if(pos + 1 >= s.size()) return;
         temp.insert(pos + 1, 1, '.');
         worker(ret, level + 1, pos + 2, temp);
         return;
     }
     for(int i = pos; i < pos + 3; ++i) {
         if(i == pos + 2) {
             if(stoi(s.substr(pos, 3)) > 255) {
                 continue;
             }
         }
         string temp = s;
         if(i + 1 >= s.size()) return;
         temp.insert(i + 1, 1, '.');
         worker(ret, level + 1, i + 2, temp);
     }
 }
Example #10
0
int main(){
    char nilai[15];
    scanf("%d %d",&r,&c);
    string cari="";
    string set ="";
    int run = 1;
    for(int i=0; i<c; i++){
        cari+="1";
        set+="0";
    }
    for(int i=0;i<r;i++){
        scanf("%s",nilai);
        string isi = nilai;
        if(isi == cari){
            run =1;
            isi = set;
            if(i > besar) besar = i;
             
        }
        data.pb(isi);
    }
    int stat= 0;
    int atas;
    while(run){
        atas=-1;
        for(int i=0;i<r-1;i++){
            if(data[i] != cari && data[i] != set){
                atas=i;
            }
        }
        if(data[r-1] == set && atas!=-1){
            stat = 1;
            besar = r-1;
        }
        runtuhkan();
        stat=0;
        besar=0;
        for(int i=0;i<r;i++){
            if(data[i] == cari){
                stat=1;
                data[i] = set;
                if(i>besar){
                    besar=i;
                    break;
                }
            }
        }
        if(data[r-1] == set && atas!=-1){
            stat = 1;
            besar = r-1;
        }
        if(stat !=1)       run=0;
         
    }     
    for(int i=0; i <r;i++){
        printf("%s\n",data[i].c_str());
    }
}
Example #11
0
File: B.cpp Project: lyoz/contest
void solve(vs names,vs towns)
{
	int n=names.size();
	vs res(n);
	map<string,int> f;
	rep(i,n){
		res[i]=shorten(names[i],towns[i]);
		f[res[i]]++;
	}
Example #12
0
//SBSI has members h, wc, bsa and vtc.
vs* format_data(double data[][5])
{
    static vs output;
    
    for(int i = 0; 
            data[i][0] != 0
            || data[i][1] != 0 
            || data[i][2] != 0 
            || data[i][3] != 0
            || data[i][4] != 0; 
            i++){      
        string* tmp = new SBSI;
        tmp->h = data[i][0];
        tmp->wc = data[i][1];
        tmp->bsa = data[i][2];
        tmp->vtc = data[i][3];
        tmp->expected = data[i][4];
        output.push_back(tmp);
        //cout << "Pushed back " << output[i]->h << endl;
    }
    return &output;
/*    char* p1 = data;
    char* p2 = p1;
    int n = 0;
    int counter = 0, length = 0;
    while ( p1[0] != '\0') {
        SBSI temp;
        while ( p1[0] != ',') {
            while(!isalpha(p1[0]))
                ++p1;
            p2 = p1+1;
            while(*p2 != '\0') {
                if(*p2 == '\t')
                    length++;
                    
                    break; //Element delimiter.
            }
        }
    } 
        

*/

}
Example #13
0
File: B.cpp Project: lyoz/contest
string fill(vs ss)
{
	int p=ss.size();
	string res;
	for(int i=0;i/p<ss[i%p].size();i++){
		char c=ss[i%p][i/p];
		res+=c=='.'?'0':c;
	}
	return res;
}
Example #14
0
pii find_king(const vs &initial_map) {
  for (int i = 0; i < initial_map.size(); i++) {
    for (int j = 0; j < initial_map[i].size(); j++) {
      if (initial_map[i][j] == 'X') {
        return make_pair(i+1, j+1);
      }
    }
  }
  // never reaches
}
Example #15
0
File: E.cpp Project: lyoz/contest
string solve_naive(vs ss)
{
	int k=ss.size(),n=ss[0].size();

	rep(i,k){
		string s=ss[0]; sort(all(s));
		string t=ss[i]; sort(all(t));
		if(s!=t)
			return "-1";
	}
Example #16
0
/*
* Returns an integer which is the index
* of a string in a vector of strings
*/
int returnIndexOfVector(vs &stringVector, string value)
{
	int iii;
	for (iii = 0; iii < stringVector.size(); iii++) {
		if (stringVector[iii] == value)	{
			return iii;
		}
	}
	return -1;
}
Example #17
0
void add (int si, int fi, int sj, int fj)
{
    string t;
    temp.clear();
    for (int i = si; i < fi; i++){
        for (int j = sj; j < fj; j++)
        {
            t.push_back(b[i][j]);
        }
        temp.push_back(t);
    }
    if (SET.find(temp) == SET.end())
        SET.insert(temp);
    else
    {
        fail = 1;
        return;
    }
    for (int i = )
}
Example #18
0
string newMemberTwo(vs existingNames, string newName)
{
	set<string> names(existingNames.begin(), existingNames.end());

	if(names.count(newName) == 0 )
		return newName;

	char testName[1024];
	int currentInt = 1;

	while(1)
	{

		sprintf(testName, "%s%d", newName.c_str(), currentInt);

		if(names.count(testName) == 0)
			return testName;
		else
			currentInt++;
	}
}
int solve(int apos,int wpos,int k)
{
	if(apos==a.size() && wpos==w.size()-1 && k) return 1;
	if(apos==a.size() || wpos==w.size()) return 0;
	int& res=dp[apos][wpos][k];
	if(res!=-1) return res;
	res=0;
	for(int i=k;i<w[wpos].size();i++)
		if(a[apos]==w[wpos][i]) res+=solve(apos+1,wpos,i+1);
	if(k) res+=solve(apos,wpos+1,0);
	return res;
}
string ColorfulStrings::getKth (int n, int k) 
{
	if(n > 8)
		return "";
	k--;
	cl(used, 0);
	strs.clear();

	generate(0, n);

	return "";
}
void work(map<vi, int> & ans, vs & s)
{
	int n = s.size(), m = s[0].size();
	rep(x, t[m])
	{
		vi key(n);
		rep(i, n) rep(j, m)
			key[i] += x / t[j] % 10 + '0' == s[i][j];
		auto p = ans.find(key);
		if(p == ans.end()) ans[key] = x;
		else p->second = -1;
	}
void printCase(vs V, vvs A, vd f){
    printf("Case Details:\n");
    printf("--------------\n");
    printf("Number Of Variables n: %d\n\n", V.size());
    printf("Rules:\n");
    printf("-------\n");
    for(int i=0; i<A.size(); i++){
        for(int j=0; j<A[i].size(); j++){
            printf("%s ", A[i][j].c_str());
        }
        printf(":: %.3lf\n", f[i]);
    }
}
Example #23
0
string makeMatch(vs namesWomen, vs answersWomen, vs namesMen, vs answersMen, string queryWoman)
{
	vp women;
	vp men;

	int size = namesWomen.size();

	for(int i = 0; i<size; i++)
	{
		women.push_back(ps(namesWomen[i], answersWomen[i]));
		men.push_back(ps(namesMen[i], answersMen[i]));
	}

	sort(women.begin(), women.end());
	sort(men.begin(), men.end());

	int compatible = 0;
	vp dates;

	for(vp::iterator it = women.begin(); it != women.end(); it++)
	{
		vp::iterator idealMale;
		string wAnswer = it->second;
		compatible = -1;

		for(vp::iterator mit = men.begin(); mit != men.end(); mit++)
		{
			string mAnswer = mit->second;
			int score = 0;
			for(int x = 0; x < wAnswer.length(); x++)
			{
				if(wAnswer[x] == mAnswer[x])
					score++;
			}

			if(score > compatible)
			{
				compatible = score;
				idealMale = mit;
			}
		}

		if(it->first == queryWoman)
			return idealMale->first;

		men.erase(idealMale);
	}

	string noMatch = "NO_MATCH";
	return noMatch;
}
Example #24
0
void find_openspaces(const vs &initial_map, pii *spaces) {
  int count = 0;
  for (int i = 0; i < initial_map.size(); i++) {
    for (int j = 0; j < initial_map[i].size(); j++) {
      if (initial_map[i][j] == '.') {
        spaces[count] = make_pair(i+1, j+1);
        count++;
        if (count == 2) {
          return;
        }
      }
    }
  }
}
Example #25
0
/*
* Outputs the predictions to file
* and returns the accuracy of the classification
*/
double printPredictionsAndCalculateAccuracy(vs &givenData, vs &predictions)
{
	ofstream outputFile;
	outputFile.open("decisionTreeOutput.txt");
	int correct = 0;
	outputFile << setw(3) << "#" << setw(16) << "Given Class" << setw(31) << right << "Predicted Class" << endl;
	outputFile << "--------------------------------------------------" << endl;
	for (int iii = 0; iii < givenData.size(); iii++) {
		outputFile << setw(3) << iii + 1 << setw(16) << givenData[iii];
		if (givenData[iii] == predictions[iii]) {
			correct++;
			outputFile << "  ------------  ";
		}
		else {
			outputFile << "  xxxxxxxxxxxx  ";
		}
		outputFile << predictions[iii] << endl;
	}
	outputFile << "--------------------------------------------------" << endl;
	outputFile << "Total number of instances in test data = " << givenData.size() << endl;
	outputFile << "Number of correctly predicted instances = " << correct << endl;
	outputFile.close();
	return (double)correct / 50 * 100;
}
Example #26
0
/*
 * function: naive_bayes::classification 对数据进行分类
 * return: 该数据最可能的目标属性值
 *
 */
std::string naive_bayes::classification(vs& data)
{
	assert((int)data.size() == num_attr - 1);
	// 为了防止溢出,以下对概率值取了对数
	int max_index = -1;
	double p_max = -1e300; // 最大概率
	vd p_targ_val; // 每个目标属性值对该数据的概率 P(data | target_attr[i])
	p_targ_val.resize(num_targ, 0.0);
	auto f = [&](double x, double u, double d)
	{// 求正态分布概率密度
		return std::exp(-(x - u) * (x - u) / (2 * d)) / sqrt(4 * std::acos(-1) * d);
	};
	for(int i = 0; i < num_targ; ++i)
	{
		auto& t = p_targ_val[i];
		t = std::log(p_target[i]); // 取对数
		for(int j = 0; j < num_attr - 1; ++j)
		{
			auto& p = p_datas[i][j];
			if(is_numeric[j])
			{
				t += std::log(f(std::stod(data[j]), p.mean_value, p.variance));
			}
			else
			{
				auto it = attr_to_int[j].find(data[j]);
				if(it == attr_to_int[j].end())
				{
					std::cerr<<"No such attribute value."<<std::endl;
					exit(1);
				}
				t += std::log(p.p_attr[it->second]);
			}
		}
	}
	// 找到最大概率值
	for(int i = 0; i < num_targ; ++i)
	{
//		std::cout<<p_targ_val[i]<<std::endl;
		if(p_max < p_targ_val[i])
			p_max = p_targ_val[i], max_index = i;
	}
	return int_to_attr[num_attr - 1][max_index];
}
Example #27
0
int main(int argc, char **argv){
	ios_base::sync_with_stdio(0);
	cin.tie(0);	
	
	int t;
	cin>>t;
	while(t--){
		cin>>c>>r;
		v.resize(r);
		memset(d,-1,sizeof(d));
		memset(vis,0,sizeof(vis));
		for(int i=0;i<r;++i)cin>>v[i];

		bool udah=false;
		for(int i=0;i<r && !udah;++i)
			for(int j=0;j<c && !udah;++j)
				if(v[i][j]=='.'){
					//cout<<i<<' '<<j<<'\n';
					x=i;
					y=j;
					d[i][j]=0;
					dfs(i,j);
					udah=true;
				}

		//cout<<x<<' '<<y<<'\n';

		memset(d,-1,sizeof(d));
		memset(vis,0,sizeof(vis));
		d[x][y]=0;
		dfs(x,y);

		//cout<<x<<' '<<y<<'\n';

		cout<<"Maximum rope length is "<<d[x][y]<<".\n";
	}

	return 0;
}
int main(int argc, char const *argv[]) {
	lli n,m;
	cin >> n >> m;
	for (lli i = 0; i < n; ++i)	{
		string s;
		cin >> s;
		vec.push_back(s);
	}
	for (lli i = 0; i < m; ++i)	{
		string s;
		cin >> s;
		ib result = binary_search(0, n, s);
		string say = (result.second) ? "SIM" : "NAO";
		cout << say << " " << result.first << endl;
		comps = 0;
		if(i == 6) break;
	}

	// cout << lessThan("alg","a") << endl;
	// cout << comps << endl;
	return 0;
}
Example #29
0
        int shortestLadder(vector <string> level, int coinRow, int coinColumn){ 
            M = level;
            n = M.size();
            m = M[0].size();
            vi move(2);
            move[0] = -1; move[1] = 1;

            for(int l = 0; l < n; l++){
                // cout<<"level = "<<l<<endl;
                fi(n) fj(m) visited[i][j] = 0;
                queue<pi> Q;
                Q.push(mp(n-1, m-1));
                visited[n-1][m-1] = 1;
                while(!Q.empty()){
                    pi p = Q.front();
                    Q.pop();
                    int x = p.first, y = p.second;
                    // cout<<x<<" "<<y<<endl;
                    if(x+1 == coinRow && y+1 == coinColumn) return l;
                    //Move horizontally
                    for(int k : move){
                        if(valid(x, y+k)){
                         Q.push(mp(x, y+k));
                         visited[x][y+k] = 1;
                        }
                    }
                    //Move vertically
                    for(int i = 1; i <= l; i++){
                        for(int k : move){
                            if(valid(x+k*i, y)){
                             visited[x+k*i][y] = 1;
                             Q.push(mp(x+k*i, y));
                            }
                        }
                    }
                }
            }
        } 
Example #30
0
void print(vs tmp) {
    for (int i = 0; i < tmp.size(); ++i)
        cout << "\"" << tmp[i] << "\"" << endl;
}