HDU 1082 矩阵乘法次数计算 写了半天搞定,很有成就感啊
2012-09-24 15:05
369 查看
这个思想本来不难,实现起来感觉有点复杂,容易出错。调试了搞定。有成就感。、
#include <iostream> #include <stack> #include <vector> #include <algorithm> using namespace std; int row[26], col[26]; char str[10000]; int count(int *a, int n) { int flag = 0; if(n == 2) return 0; for(int i = 1 ; i < n-1; i += 2) { if(a[i] != a[i+1]) { flag = 1; break; } } if(flag == 1) return -1; int mul = a[0]; for(int i = 1; i < n; i += 2 ) { mul *= a[i]; } return mul; } int main() { int t; cin >> t; char temp; for(int i = 0 ;i < t ; i++) { getchar(); //重要 scanf("%c", &temp); scanf("%d%d", &row[temp-'A'], &col[temp-'A']); } getchar(); while(gets(str)) { int n = strlen(str); if(n == 1) { cout << "0" << endl; continue; } stack<char> stk; int sum = 0; int flag = 0; for(int i = 0; i < n; i++) { if(str[i] != ')' ) { if(isalpha(str[i])) { stk.push((char)row[str[i] - 'A']); stk.push((char)col[str[i] - 'A']); } if(str[i] == '(') stk.push('('); } if(str[i] == ')' || i == n-1) { int cnt = 0; int a[10000]; while(!stk.empty()) { if(stk.top() == '(') { stk.pop(); break; } a[cnt++] = (int)stk.top(); stk.pop(); } reverse(a, a+ cnt); if(!stk.empty()) { stk.push(a[0]); stk.push(a[cnt-1]); } int mul = 1; mul = count(a, cnt); if(mul == -1) { flag = 1; break; } else sum += mul; } if(flag == 1) break; } if(flag == 1) cout << "error" << endl ; else cout << sum << endl; } return 0; }
相关文章推荐
- stack应用--矩阵乘法次数计算
- 计算矩阵乘法所需运算的次数
- 计算矩阵运算的乘法次数
- 杭电 1082【矩阵乘法次数的运算】
- 计算矩阵运算的乘法次数
- 2013 多校第九场 hdu 4686 Arc of Dream(矩阵乘法或者直接推公式)
- hdu 3893 Drawing Pictures 矩阵乘法
- hdu 4920 Matrix multiplication(矩阵乘法)
- Strassen矩阵乘法 + 快速计算乘方的算法 + 矩阵的次幂
- hdu 2842 Chinese Rings(矩阵乘法+递推)
- 华为OJ矩阵乘法计算量估算
- 【hdu】【P2157】【How many ways??】【题解】【矩阵乘法】
- hdu 4686 矩阵乘法优化递推关系
- HDU 2276 小小矩阵乘法
- 矩阵链乘法确定矩阵的最佳计算顺序
- HDU 2865 Birthday Toy [Polya 矩阵乘法]
- 数据结构——矩阵压缩与压缩矩阵的转置与乘法计算
- 手把手教你用Execel计算两个矩阵的乘法
- 矩阵连乘时最少乘法次数
- 分布式缓存法计算矩阵乘法