题目要读很久才能理解它的意思和笑点(如果你也看过那个笑话的话),读懂之后就会发现是一个高斯消元法的题目,对于我来说难点不在高斯消元,而在于字符串处理。先来说说题意吧:

总共有n个人,n个人都会有一段话,先是princess说话,里面如果提到了a1,a2,a3...这几个不同的人的话,对应提到的次数是x1,x2,x3..的话,那么下一个对话是ai这个人说的概率是xi/(x1+x2+x3)....,然后下一个人的对话里也会提到别的人,然后也有一定的概率会有下一轮对话,现在要问的就是,给定了这些对话,问你期望的对话次数是多少。

我们可以设第i个人持续的对话的期望是xi,那么xi应该等于  xi=p1*x1+p2*x2+p3*x3+...+1 对应的pi即为在该人对话出现的次数所对应的频率。然后对每个人列出这样的方程就会构成一系列的方程组。然后高斯消元即可。

难度在于算在某个对话里出现了多少次,因为像如何你用KMP去做匹配 prince是会出现在princess那里的,题目说了人名之间是用五种间隔符隔开的,所以匹配的时候或者匹配前将对话里的每一个词分出来,然后和对应的字符串判相等即可。我写的不好跪了好几发呀- -0

#pragma warning(disable:4996)
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<vector>
#define ll long long
#define maxn 220
#define eps 1e-7
using namespace std; char str[120][1050];
char name[120][20];
int n; double mat[120][120];
char word[1200]; int dcmp(double x) {
return (x > eps) - (x < -eps);
} int cnt(char*T, char *S)
{
int res = 0; int idx = 0;
for (int i = 0; T[i]; i++){
if (!(T[i] <= 'z'&&T[i] >= 'a')) continue;
int id = 0;
while (T[i] <= 'z'&&T[i] >= 'a'){
word[id++] = T[i++];
}
word[id] = '\0';
if (strcmp(word, S) == 0) res++;
i--;
}
return res;
} bool gauss(double mat[120][120], int n)
{
for (int i = 0; i < n; i++){
int pivot = i;
for (int j = i; j < n; j++){
if (abs(mat[j][i])>abs(mat[pivot][i])) pivot = j;
}
swap(mat[i], mat[pivot]);
if (abs(mat[i][i]) < eps) return false;
for (int j = i + 1; j <= n; j++) mat[i][j] /= mat[i][i];
for (int j = 0; j < n; j++){
if (i != j)
for (int k = i + 1; k <= n; k++){
mat[j][k] -= mat[j][i] * mat[i][k];
}
}
}
return true;
} int main()
{
while (cin >> n)
{
getchar();
memset(name, 0, sizeof(name));
memset(str, 0, sizeof(str));
for (int i = 0; i < n; i++){
gets(str[i]);
int j;
for (j = 0; str[i][j]; j++){
if (str[i][j] == ':') break;
name[i][j] = str[i][j];
}
name[i][j + 1] = '\0'; int k = 0; j++;
for (; str[i][j]; j++,k++){
str[i][k] = str[i][j];
}
str[i][k] = '\0';
}
memset(mat, 0, sizeof(mat));
for (int i = 0; i < n; i++){
double tot = 0; double tmp = 0;
for (int j = 0; j < n; j++){
if (i == j) continue;
tmp = cnt(str[i], name[j]);
mat[i][j] = tmp;
tot += tmp;
}
mat[i][i] = -tot; mat[i][n] = -tot;
if (dcmp(tot) == 0){
mat[i][i] = mat[i][n] = -1;
}
}
if (gauss(mat, n)){
printf("%.3lf\n", mat[0][n]);
}
else puts("Infinity");
}
return 0;
}

ZOJ3560 Re:the Princess(高斯消元法)的更多相关文章

  1. POJ 1753. Flip Game 枚举or爆搜+位压缩,或者高斯消元法

    Flip Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 37427   Accepted: 16288 Descr ...

  2. HDU--杭电--1026--Ignatius and the Princess I--广搜--直接暴力0MS,优先队列的一边站

    别人都是广搜+优先队列,我没空临时学,所以就直接自己暴力了 Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others)     ...

  3. hdu 1026(Ignatius and the Princess I)BFS

    Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (J ...

  4. hdu acm 1028 数字拆分Ignatius and the Princess III

    Ignatius and the Princess III Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K ...

  5. hdu1026.Ignatius and the Princess I(bfs + 优先队列)

    Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (J ...

  6. sdut 2603:Rescue The Princess(第四届山东省省赛原题,计算几何,向量旋转 + 向量交点)

    Rescue The Princess Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 Several days ago, a b ...

  7. hdu 1029 Ignatius ans the Princess IV

    Ignatius and the Princess IV Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32767 K ( ...

  8. POJ 1222 (开关问题+高斯消元法)

    题目链接: http://poj.org/problem?id=1222 题目大意:一堆开关,或开或关.每个开关按下后,周围4个方向开关反转.问使最后所有开关都关闭的,开关按法.0表示不按,1表示按. ...

  9. ACM: HDU 1028 Ignatius and the Princess III-DP

     HDU 1028 Ignatius and the Princess III Time Limit:1000MS     Memory Limit:32768KB     64bit IO Form ...

随机推荐

  1. WPF数据双向绑定

    设置双向绑定,首先控件要绑定的对象要先继承一个接口: INotifyPropertyChanged 然后对应被绑定的属性增加代码如下: 意思就是当Age这个属性变化时,要通知监听它变化的人. 即:Pr ...

  2. 博客导出工具(C++实现,支持sina,csdn,自定义列表)

    操作系统:windowAll 编程工具:visual studio 2013 编程语言:VC++ 最近博文更新的较频繁,为了防止账号异常引起csdn博文丢失,所以花了点时间做了个小工具来导出博文,用做 ...

  3. Java入门到精通——工具篇之Maven概述

    为接手gxpt准备已经快一个月了从SSH2-->EJB-->环境搭建-->Maven的构建.下面就带领大家初始Maven 一.什么是Maven. Maven是一个垮平台的项目管理工具 ...

  4. DB2中ixf文件的导入导出

    1. 导出数据 语法:EXPORT TO <文件路径>/文件名.IXF OF IXF SELECT * FROM 表名   2. 导入数据 语法:db2 IMPORT FROM <路 ...

  5. 通过Maven搭建Mybatis项目

    学习通过maven工程搭建Mybatis工程开启对M ybaits的学习总结之旅. 1.首先创建Maven工程. 2.在pom.xml文件中加入依赖的jar <!-- mybatis核心包 -- ...

  6. C# 获取图片的EXIF 信息

    关于 EXIF 信息的介绍. 1  EXIF,是英文Exchangeable Image File(可交换图像文件)的缩写.EXIF是一种图像文件格式,只是文件的后缀名为jpg.EXIF信息是由数码相 ...

  7. Linux 文件与目录

    文件描述符 在内核中,所有打开的文件都使用文件描述符(一个非负整数)标记.文件描述符的变化范围是0~OPEN_MAX – 1.早期的unix系统中,每个进程最多可以同时打开20个文件,就是说文件描述符 ...

  8. [转]Cygwin的包管理器:apt-cyg

    [转]Cygwin的包管理器:apt-cyg http://zengrong.net/post/1792.htm Cygwin的包管理工具setup.exe实在是难用的让人蛋碎.于是就有了这样一个ap ...

  9. WIN7笔记本利用命令AP热点

    第一步:以管理员身份运行命令提示符:开始”---搜索栏输入“cmd----右键以“管理员身份运行”自己随便命名,第二步:运行命令:netsh wlan set hostednetwork mode=a ...

  10. 如何解决读取到文件末尾时碰到EOF导致的重复输出或者无用输出

    当读取到文件末尾时,会碰到EOF,如何解决呢?    方法一:我们可以通过(ch=fin.get())!=EOF来结束读取,这样就不会像eof()那样碰到EOF之后,还会再进行一次读取,导致输出一个无 ...