898 C. Phone Numbers
传送门
[http://codeforces.com/contest/898/problem/C]
题意
题意比较难理解直接看样例就知道了,给你个n接下来n行,每行包括一个名字和号码的数量,还有具体的每个号码
让你整理他的电话本,使得一个人的号码不能有重复,而且一个号码不能是另一个号码的后缀
分析
首先可以对一个人的所有号码去重,用map<string,set<string>> m来存信息可以,set具有惟一性,然后输出m.size()即为所有联系人
然后用三重循环来处理使得一个人的号码不能有重复,而且一个号码不能是另一个号码的后缀,关键是判断一个字符串是否是另一个字符串的
后缀,substr()函数
1. 用途:一种构造string的方法
2. 形式:s.substr(pos, n)
3. 解释:返回一个string,包含s中从pos开始的n个字符的拷贝(pos的默认值是0,n的默认值是s.size() - pos,即不加参数会默认拷贝整个s)
4. 补充:若pos的值超过了string的大小,则substr函数会抛出一个out_of_range异常;若pos+n的值超过了string的大小,则substr会调整n的值,只拷贝到string的末尾
例如:
#include<string>
#include<iostream>
using namespace std;
int main()
{
string s("12345asdf");
string a = s.substr(0,5); //获得字符串s中从第0位开始的长度为5的字符串
cout << a << endl;
}
输出结果为:12345
本题代码
#include<bits/stdc++.h>
using namespace std;
int main(){
int n;
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
//freopen("in.txt","r",stdin);
while(cin>>n){
map<string,set<string> > m;
int num;
string name;
for(int i=0;i<n;i++){
cin>>name>>num;
string s;
for(int j=0;j<num;j++){
cin>>s;
m[name].insert(s);
}
}
cout<<m.size()<<endl;
for(map<string,set<string> >::iterator it=m.begin();it!=m.end();it++){
string name=(*it).first;
vector<string> v;
for(set<string>::iterator at=m[name].begin();at!=m[name].end();at++){
string a=*at;
bool flag=1;
for(set<string>::iterator bt=m[name].begin();bt!=m[name].end();bt++){
string b=*bt;
if(a==b||a.length()>b.length()) continue;
if(b.substr(b.length()-a.length(),a.length())==a)
{
flag=0; break;
}
}
if(flag) v.push_back(a);
}
cout<<name<<' '<<v.size()<<' ';
for(int i=0;i<v.size();i++)
cout<<v[i]<<' ';
cout<<endl;
}
}
return 0;
}
898 C. Phone Numbers的更多相关文章
- Java 位运算2-LeetCode 201 Bitwise AND of Numbers Range
在Java位运算总结-leetcode题目博文中总结了Java提供的按位运算操作符,今天又碰到LeetCode中一道按位操作的题目 Given a range [m, n] where 0 <= ...
- POJ 2739. Sum of Consecutive Prime Numbers
Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20050 ...
- [LeetCode] Add Two Numbers II 两个数字相加之二
You are given two linked lists representing two non-negative numbers. The most significant digit com ...
- [LeetCode] Maximum XOR of Two Numbers in an Array 数组中异或值最大的两个数字
Given a non-empty array of numbers, a0, a1, a2, … , an-1, where 0 ≤ ai < 231. Find the maximum re ...
- [LeetCode] Count Numbers with Unique Digits 计算各位不相同的数字个数
Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. Examp ...
- [LeetCode] Bitwise AND of Numbers Range 数字范围位相与
Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers ...
- [LeetCode] Valid Phone Numbers 验证电话号码
Given a text file file.txt that contains list of phone numbers (one per line), write a one liner bas ...
- [LeetCode] Consecutive Numbers 连续的数字
Write a SQL query to find all numbers that appear at least three times consecutively. +----+-----+ | ...
- [LeetCode] Compare Version Numbers 版本比较
Compare two version numbers version1 and version1.If version1 > version2 return 1, if version1 &l ...
随机推荐
- 机器学习中学习曲线的 bias vs variance 以及 数据量m
关于偏差.方差以及学习曲线为代表的诊断法: 在评估假设函数时,我们习惯将整个样本按照6:2:2的比例分割:60%训练集training set.20%交叉验证集cross validation set ...
- JMeter乱码常见的解决方案
方法一.直接将JMeter中http请求中Content encoding改为utf-8 方法二.编辑JMeter安装目录:apache-jmeter-3.2\bin中的jmeter.properti ...
- python----运算符、布尔值
一.运算符: + - * / ** // % 1,. in ,not in 用法(判断某个东西是否在某个东西里面) name = '郑建文' 其中‘郑建文’是字符串, ‘郑’或‘建’或‘文’是一个 ...
- account
Account Doc V3_ADD 1. 用户头像 用户头像今后会放在阿里云上,所以: dev: http(s)://pyserver.oss-cn-hangzhou.aliyuncs.com/DE ...
- ping 127.0.0.1和ping本地ip分别测试什么?
ping 127.0.0.1 是你本地的回环地址! 实际上只要是127.0.0.1到127.255.255.255都是回环地址!都是可以PING检查的! 它能ping通,说明你的TCP/IP协议栈没问 ...
- Session共享的四种方法
1. 基于NFS的Session共享 NFS是Net FileSystem的简称,最早由Sun公司为解决Unix网络主机间的目录共享而研发. 这个方案实现最为简单,无需做过多的二次开发,仅需将共享目录 ...
- 【转】Android 创建AVD各参数详解
一.Eclipse中图形创建AVD: Device: 即设备,指具体的手机设备型号,可以在window->Android Virtual Device Manager->Device De ...
- day16 Python 内置函数 大体演示想看就看,会用就行
1.abs() 获取绝对值 a = -10 print(a.__abs__()) 结果: 10 2.all() 接收一个迭代器,如果跌电气的所有元素都为真,那么返回True,否则返回False tm ...
- 新手根据菜鸟教程安装docker,从No package docker-io available开始遇到的坑...(转)
转文地址:https://www.cnblogs.com/maodot/p/7654918.html 新手centos6.9安装docker时从遇到No package docker-io avail ...
- Electron 发生错误 "Cannot find module app"的解决方案
运行一个electron小demo出现的一个错误信息:Cannot find module app 原代码如下所示: var app = require('app'); var BrowserWind ...