[F] Teacher's Problem(处理大数时,优化很重要)
2014-02-13 08:43
337 查看
1、http://acm.nbut.edu.cn/Problem/view.xhtml?id=1546
2、题目大意:
有n个字符串,每个字符串都有一个价值,如果第i个字符串是第j个字符串的前缀,那么第i个字符串就是第j个字符串的父亲,这样就会组成一棵树,子节点的价值都更新到父节点的价值上,然后按顺序输出即可
3、题目:
问题描述
One day my teacher gave me a problem to solve. I was given N unique names andeach name has a value. Name is a string consisting of lowercase Latin letters withlength less than
10. Value is a positive integer less than 100. Initially these namesare sorted in lexicographic order. Now if the j-th name is the prefix of the i-th nameand has the max
length, j is thefather of i. Then numbers 1 to N form a tree. Fromleafto
root, each son's value should be added to father's value. After I added thesevalues, my teacher ask me to show allthese values.
Can you help me with the problem?
输入
First line there is an integer T (T < 20) following T test cases.
For each test case, first line is an integer N (0 < N <= 10^5), second line is N names, third line is N values.
Names and values are separate by a space. There is no space at the end of line.
Names are all unique and are given in Lexicographic order.
输出
For each test case, please output one line with N integers. The i-th integer is the i-th value after added.
Please do not output a space at the end of line.
样例输入
样例输出
提示
来源
4、Ac代码:
2、题目大意:
有n个字符串,每个字符串都有一个价值,如果第i个字符串是第j个字符串的前缀,那么第i个字符串就是第j个字符串的父亲,这样就会组成一棵树,子节点的价值都更新到父节点的价值上,然后按顺序输出即可
3、题目:
[F] Teacher's Problem
时间限制: 2000 ms 内存限制: 262144 K问题描述
One day my teacher gave me a problem to solve. I was given N unique names andeach name has a value. Name is a string consisting of lowercase Latin letters withlength less than
10. Value is a positive integer less than 100. Initially these namesare sorted in lexicographic order. Now if the j-th name is the prefix of the i-th nameand has the max
length, j is thefather of i. Then numbers 1 to N form a tree. Fromleafto
root, each son's value should be added to father's value. After I added thesevalues, my teacher ask me to show allthese values.
Can you help me with the problem?
输入
First line there is an integer T (T < 20) following T test cases.
For each test case, first line is an integer N (0 < N <= 10^5), second line is N names, third line is N values.
Names and values are separate by a space. There is no space at the end of line.
Names are all unique and are given in Lexicographic order.
输出
For each test case, please output one line with N integers. The i-th integer is the i-th value after added.
Please do not output a space at the end of line.
样例输入
2 3 a ab ac 5 10 15 5 a ab abb ac b 5 10 100 15 6
样例输出
30 10 15 130 110 100 15 6
提示
Huge input, scanf is recommended.
来源
zzxchavo @HEU
4、Ac代码:
#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; #define N 100005 char s [12]; int w ; int check(int a,int b) { int len=strlen(s[a]); for(int i=0;i<len;i++) { if(s[a][i]!=s[b][i]) return 0; } return 1; } int main() { int t,n; scanf("%d",&t); while(t--) { scanf("%d",&n); for(int i=1;i<=n;i++) { scanf("%s",s[i]); } for(int i=1;i<=n;i++) scanf("%d",&w[i]); for(int i=1;i<n;i++) { int sum=0; sum+=w[i]; for(int j=i+1;j<=n;j++) { if(check(i,j)) sum+=w[j]; else break; } printf("%d ",sum); } printf("%d\n",w ); } return 0; }
相关文章推荐
- 未能处理对象 'select * from teacher.dbf'。OLE DB 提供程序 'microsoft.JET.OLEDB.4.0' 指出该对象中没有任何列。
- flume nginx 日志处理异常 JsonParseException: Unexpected character ('(' (code 40)): expected a valid value
- Run-time error '10054':The connection is reset by remote side的处理
- UVA 10026 Shoemaker's Problem
- Maximum function nesting level of '100' reached, aborting!--漫谈递归:PHP里的尾递归及其优化
- 关于error C2871: ''std'' : does not exist or is not a namespace处理
- [导入]I'll accept every aspects of being a teacher^_^ thank you Mark!
- hdu 5186 zhx's submissions 5187 zhx's contest快速幂小优化
- JDK5.0垃圾收集优化之--Don't Pause
- HDU 1250 - Hat's Fibonacci(大数斐波那契)
- python脚本处理文本时遇到的字符问题:UnicodeDecodeError: " " codec can't decode byte
- 2480 Longge's problem 欧拉函数变形 求,∑gcd(i, N)
- javax.el.PropertyNotFoundExceptionProperty 'Tid' not found on type com.msz.sims.domain.Teacher
- HDU 1250 Hat's Fibonacci (Java大数,类似Fib数,有个坑!!)
- POJ 1681 Painter's Problem 高斯消元+DFS枚举
- Acdream 1210 Chinese Girls' Amusement(大数模板运算 + 找规律)
- poj 1681 Painter's Problem(高斯消元)
- poj 1681 Painter's Problem(高斯消元 开关问题 )
- [mysql]重装windows后恢复mysql及"Access denied for user'root'@'IP'"问题的处理
- 处理使用node-gpy时遇到的Can't find "msbuild.exe"错误