CSUOJ 1826 Languages map+stringstream

Description

The Enterprise has encountered a planet that at one point had been inhabited. The onlyremnant from the prior civilization is a set of texts that was found. Using a small set of keywordsfound in various different languages, the Enterprise team is trying to determine what type of beingsinhabited the planet.

Input

The first line of input will be N (1 ≤ N ≤ 100), the number of different known languages. Thenext N lines contain, in order, the name of the language, followed by one or more words in thatlanguage, separated with spaces. Following that will be a blank line. After that will be a series oflines, each in one language, for which you are to determine the appropriate language.Words consist of uninterrupted strings of upper or lowercase ASCII letters, apostrophes, orhyphens, as do the names of languages. No words will appear in more than one language.No line will be longer than 256 characters. There will be at most 1000 lines of sample text.Every sample text will contain at least one keyword from one of the languages. No sampletext will contain keywords from multiple languages. The sample text may contain additionalpunctuation (commas, periods, exclamation points, semicolons, question marks, and parentheses)and spaces, all of which serve as delimiters separating keywords. Sample text may contain wordsthat are not keywords for any specific language.Keywords should be matched in a case-insensitive manner.

Output

For each line of sample text that follows the blank line separating the defined languages, print asingle line that identifies the language with which the sample text is associated.

Sample Input

4
Vulcan throks kilko-srashiv k'etwel
Romulan Tehca uckwazta Uhn Neemasta
Menk e'satta prah ra'sata
Russian sluchilos

Dif-tor heh, Spohkh. I'tah trai k'etwel
Uhn kan'aganna! Tehca zuhn ruga'noktan!

Sample Output

Vulcan
Romulan

Hint

题意:告诉你每种语言的一些单词,再给一个段话,判断是用什么语言写的。
又一次感受到C++ stl的强大

思路:使用 stringstream类读取字符串,通过>>操作分离出单词,然后进行判断 

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<sstream>
#include<string>
#include<string.h>
#include<ctype.h>
#include<map>
using namespace std;
map<string, string>m;
void lower(string &s)
{
	for (int i = 0; i < s.length(); i++)
	{
		s[i] = tolower(s[i]);
	}
}
int main()
{
	int T;
	m.clear();
	string a, b,name,s;
	cin >> T;
	getchar();
	for (int i = 0; i < T; i++)
	{
		getline(cin, a);
		stringstream ss(a);
		ss >> name;
		while (ss >> b)
		{
			lower(b);
			m[b] = name;
		}
	}
	while (getline(cin, s))
	{
		string tmp;
		for (int i = 0; i < s.length(); i++)
		{
			if (s[i] == ',' || s[i] == '.' || s[i] == '!' || s[i] == ';' || s[i] == ' ? ' || s[i] == '(' || s[i] == ')')
				s[i] = ' ';
		}
		stringstream ss1(s);
		while (ss1 >> tmp)
		{
			lower(tmp);
			if (m.count(tmp))
			{
				cout << m[tmp] << endl;
				break;
			}
		}
	}
	return 0;
}
/**********************************************************************
	Problem: 1826
	User: leo6033
	Language: C++
	Result: AC
	Time:68 ms
	Memory:2348 kb
**********************************************************************/

首先,给出Student类的完整代码: ```java public class Student { private String sNo; private String sName; private String[] languages; private List<String> favors; private Set<String> s; private Map<String, Float> scores; public Student() { } public Student(String sNo, String sName, String[] languages, List<String> favors, Set<String> s, Map<String, Float> scores) { this.sNo = sNo; this.sName = sName; this.languages = languages; this.favors = favors; this.s = s; this.scores = scores; } public void setsNo(String sNo) { this.sNo = sNo; } public void setsName(String sName) { this.sName = sName; } public void setLanguages(String[] languages) { this.languages = languages; } public void setFavors(List<String> favors) { this.favors = favors; } public void setS(Set<String> s) { this.s = s; } public void setScores(Map<String, Float> scores) { this.scores = scores; } @Override public String toString() { return "Student{" + "sNo='" + sNo + '\'' + ", sName='" + sName + '\'' + ", languages=" + Arrays.toString(languages) + ", favors=" + favors + ", s=" + s + ", scores=" + scores + '}'; } } ``` 然后,在beans.xml中使用多种注入方式完成对象及属性赋值: ```xml <bean id="student1" class="com.example.Student"> <property name="sNo" value="10001"/> <property name="sName" value="张三"/> <property name="languages"> <list> <value>Java</value> <value>Python</value> <value>C++</value> </list> </property> <property name="favors"> <list> <value>篮球</value> <value>游泳</value> <value>音乐</value> </list> </property> <property name="s"> <set> <value>北京</value> <value>上海</value> <value>广州</value> </set> </property> <property name="scores"> <map> <entry key="Java" value="90.5"/> <entry key="Python" value="80.0"/> <entry key="C++" value="85.5"/> </map> </property> </bean> <bean id="student2" class="com.example.Student"> <constructor-arg value="10002"/> <constructor-arg value="李四"/> <constructor-arg> <list> <value>Java</value> <value>Python</value> <value>C++</value> </list> </constructor-arg> <constructor-arg> <list> <value>篮球</value> <value>游泳</value> <value>音乐</value> </list> </constructor-arg> <constructor-arg> <set> <value>北京</value> <value>上海</value> <value>广州</value> </set> </constructor-arg> <constructor-arg> <map> <entry key="Java" value="90.5"/> <entry key="Python" value="80.0"/> <entry key="C++" value="85.5"/> </map> </constructor-arg> </bean> <bean id="student3" class="com.example.Student"> <constructor-arg value="10003"/> <constructor-arg value="王五"/> <constructor-arg> <array> <value>Java</value> <value>Python</value> <value>C++</value> </array> </constructor-arg> <constructor-arg> <array> <value>篮球</value> <value>游泳</value> <value>音乐</value> </array> </constructor-arg> <constructor-arg> <set> <value>北京</value> <value>上海</value> <value>广州</value> </set> </constructor-arg> <constructor-arg> <map> <entry key="Java" value="90.5"/> <entry key="Python" value="80.0"/> <entry key="C++" value="85.5"/> </map> </constructor-arg> </bean> ``` 最后,编写Test.java文件进行测试: ```java public class Test { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); Student student1 = context.getBean("student1", Student.class); System.out.println(student1); Student student2 = context.getBean("student2", Student.class); System.out.println(student2); Student student3 = context.getBean("student3", Student.class); System.out.println(student3); } } ``` 运行结果如下: ``` Student{sNo='10001', sName='张三', languages=[Java, Python, C++], favors=[篮球, 游泳, 音乐], s=[北京, 上海, 广州], scores={Java=90.5, Python=80.0, C++=85.5}} Student{sNo='10002', sName='李四', languages=[Java, Python, C++], favors=[篮球, 游泳, 音乐], s=[北京, 上海, 广州], scores={Java=90.5, Python=80.0, C++=85.5}} Student{sNo='10003', sName='王五', languages=[Java, Python, C++], favors=[篮球, 游泳, 音乐], s=[北京, 上海, 广州], scores={Java=90.5, Python=80.0, C++=85.5}} ``` 可以看出,三个学生对象的属性值都已经正确地注入到了Bean中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值