Programming Beauty Warm-up relay game

Source: Internet
Author: User
Tags stdin

Through this problem, I found that the processing power of string is poor. This morning to write this problem found reading the string has been read not to go in, and then in front of the Getline plus cin.get finally read the string into the. Then there is the case of garbled, and then found that it is from the character assignments value to the string when there is no end, the string type after the addition of a pair of strange characters, plus the limit statement after the good. Then it is submitted always WA. After careful examination, it is found that the Getline function has set the number of input characters to 100. Just start thinking about the topic said that the number of characters in each sentence is not more than 100, and then suddenly understand that the title of this thing refers to not include spaces. And then changed the 100 to 1000. The next submission is re. And then check, found that the problem is given the M maximum is 100, that is, the word conversion list length of 100 lines, the beginning of the graph array opened 100*100. But there are two words in each line, which means the maximum can be 200 different words. Then the space of the graph expands, finally passed.

The code is as follows:

#include <iostream> #include <cstring> #include <vector> #include <cstdio> using namespace std;
The int graph[205][205];//begins by opening the array 105 to cause re vector<string> words;
	int ID (const string name) {string s (name);
	int n=words.size ();
	for (int i=0;i<n;++i) {if (s==words[i]) return i;
	} words.push_back (name);
return n;
	int GetID (const string word) {string s (word);
	int n=words.size ();
	for (int i=0;i<n;++i) {if (s==words[i]) return i;
} return-1;
	int main () {//Freopen ("Data.txt", "R", stdin);
	Ios::sync_with_stdio (FALSE);
	int T;
	cin>>t;
	int kace=0;
		while (t--) {cout<< "Case #" <<++kace<< ': ' << ';
		Words.clear ();
		memset (graph,0,sizeof (graph));
		int n,m;
		cin>>n>>m;
			for (int i=0;i<m;++i) {string from;
			string to;
			cin>>from>>to;
			int F=id (from);
			int T=id (to);
		Graph[f][t]=1;		}//for (int i=0;i<words.size (); ++i) {//cout<<i<< ' <<words[i]<<endl;/}//for (int i=0;i<words.size (); ++i) {//for (int t=0;t<words.size (); ++t) {//cout<<graph[i][t]<< '
';
}//cout<<endl;
		} Char mes[105];
		for (int i=0;i<101;++i) {mes[i]= ' ";}
		Cin.get ();
		Cin.getline (mes,1000);//Here is the first 100, which causes WA int tar=0;
			while (Tar<strlen (MES)) {string speak;
			Speak.clear ();
				while (mes[tar]!= ' &&mes[tar]!= ' \ n ' &&tar<strlen (MES)) {//Here at first only one judgment condition mes[tar]!= ' caused the string garbled
				Speak+=mes[tar];
			++tar;
			int POS;
				for (int i=0;i<n-1;++i) {Pos=getid (speak);
				if (pos==-1) {cout<<speak;break;}
				int findw=0;
				for (int t=0;t<words.size (); ++t) {if (graph[pos][t)) {findw=1;speak=words[t];p os=t;break;}
				}//cout<< "11speak=" <<speak<<endl;
				if (!FINDW) {cout<<speak;break;}
			if (i==n-2) {cout<<speak;}
			} ++tar;
		if (Tar<strlen (MES)) cout<< ';
		} fflush (stdin);
	cout<<endl;
return 0; }


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.