⭐201509-3 template generation system

Put the link: template generation system
I feel that the tradition of examining string operations in the third question has been left since this question

#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<cstdio>
#include<string>
#include<vector>
#include<queue>
#include<stack>
#include<algorithm>
#include<map>
using namespace std;
vector<string> str;
string tempstr;
string str1, str2;
map<string, string> mp;
int n, m;
int main() {
    
    
	scanf("%d %d",&m,&n);
	getchar();
	for (int i = 0; i < m; i++)
	{
    
    
		getline(cin,tempstr);
		str.push_back(tempstr);
	}
	
	for (int i = 0; i < n; i++)
	{
    
    
		getline(cin,tempstr);		
		int t1 = tempstr.find("\"");		
		str1 = tempstr.substr(0,t1-1);

		tempstr.erase(0,t1+1);
		int t2 = tempstr.find("\"");
		str2 = tempstr.substr(0,t2);

		mp[str1] = str2;		
	}
	map<string, string>::iterator it;
	/*for ( it = mp.begin(); it!=mp.end(); it++)
	{
		cout << it->first << " " << it->second << endl;
	}*/
	for (int i = 0; i < m; i++)
	{
    
    
		int pos1, pos2, pos3;
		for ( pos1 = str[i].find("{
    
    { "); pos1 != -1; pos1 = str[i].find("{
    
    { ",pos1))
		{
    
    	
			int flag=0;
			for (it = mp.begin(); it != mp.end(); it++)
			{
    
    
				 pos2 = str[i].find(it->first, pos1);
				if (pos2!=-1)//如果找到了可以替代的字符串
				{
    
    
					str[i].replace(pos1, it->first.length()+6, it->second);
					pos1 = pos1 + it->second.length();
					flag = 1;
					break;
				}
			}
			pos3 = str[i].find(" }}");
			if (flag == 0)//没有找到替换
			{
    
    				
				str[i].erase(str[i].begin()+pos1,str[i].begin()+pos3+3);
			}			
		}	
		
	}

	for (int i = 0; i < m; i++)
	{
    
    
		cout<<str[i]<<endl;
	}
	return 0;
}

The above code can only score 90 points, I don't understand why

Later, I referred to the problem solution and modified it.
Wissenmo also uses STL, and dl can be used so concisely and clearly! Even Liao himself has made progress, at least he can use STL

#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<cstdio>
#include<string>
#include<vector>
#include<queue>
#include<stack>
#include<algorithm>
#include<map>
using namespace std;
vector<string> str;
string tempstr;
string str1, str2;
map<string, string> mp;
int n, m;
int main() {
    
    
	scanf("%d %d",&m,&n);
	getchar();
	for (int i = 0; i < m; i++)
	{
    
    
		getline(cin,tempstr);
		str.push_back(tempstr);
	}
	
	for (int i = 0; i < n; i++)
	{
    
    
		getline(cin,tempstr);		
		int t1 = tempstr.find("\"");		
		str1 = tempstr.substr(0,t1-1);

		tempstr.erase(0,t1+1);
		int t2 = tempstr.find("\"");
		str2 = tempstr.substr(0,t2);

		mp[str1] = str2;		
	}
	map<string, string>::iterator it;
	/*for ( it = mp.begin(); it!=mp.end(); it++)
	{
		cout << it->first << " " << it->second << endl;
	}*/
	for (int i = 0; i < m; i++)
	{
    
    
		int prev, next;
		prev = 0;
		while(1)
		{
    
    
			if ((prev = str[i].find("{
    
    { ", prev)) == (int)string::npos)
				break;
			if ((next = str[i].find(" }}", prev)) == (int)string::npos)
				break;
			string key = str[i].substr(prev + 3, next - prev - 3);
			str[i].replace(prev, next - prev + 3, mp.count(key) ? mp[key] : "");
			prev += mp.count(key) ? mp[key].length() : 0;   // 避免重复替换,因为替换的关键词里面可能有{
    
    {
    
    
		}		
		
	}

	for (int i = 0; i < m; i++)
	{
    
    
		cout<<str[i]<<endl;
	}
	return 0;
}

Guess you like

Origin blog.csdn.net/susuate/article/details/120117315